Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Element color in custom style
Forum Updated to NodeBB v4.3 + New Features

Element color in custom style

Scheduled Pinned Locked Moved Solved General and Desktop
styleqproxystylqstylepaintercolor
3 Posts 2 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gabodev
    wrote on 25 Feb 2017, 18:13 last edited by
    #1

    Hi!
    I'm creating a custom style from QProxyStyle and I need the arrow indicator to be white because the background is dark:

    def drawPrimitive(self, element, opt, painter, widget):
            if element == QStyle.PE_PanelButtonTool:
                pressed = (opt.state & STATE_SUNKEN | opt.state & STATE_ON)
                color = QColor("#323232")
                if pressed:
                    color = QColor("#222222")
                elif opt.state & STATE_ENABLED and opt.state & STATE_MOUSEOVER:
                    color = QColor("#4e4e4e")
                painter.fillRect(opt.rect, color)
            elif element == QStyle.PE_IndicatorArrowDown:
                painter.setBrush(QColor("white"))
                QProxyStyle.drawPrimitive(self, element, opt, painter, widget)
            else:
                QProxyStyle.drawPrimitive(self, element, opt, painter, widget)
    
    


    But that doesn't work, I'm forgetting something?

    Thanks!

    Developer at Ninja-IDE

    M 1 Reply Last reply 25 Feb 2017, 18:26
    0
    • G gabodev
      25 Feb 2017, 18:13

      Hi!
      I'm creating a custom style from QProxyStyle and I need the arrow indicator to be white because the background is dark:

      def drawPrimitive(self, element, opt, painter, widget):
              if element == QStyle.PE_PanelButtonTool:
                  pressed = (opt.state & STATE_SUNKEN | opt.state & STATE_ON)
                  color = QColor("#323232")
                  if pressed:
                      color = QColor("#222222")
                  elif opt.state & STATE_ENABLED and opt.state & STATE_MOUSEOVER:
                      color = QColor("#4e4e4e")
                  painter.fillRect(opt.rect, color)
              elif element == QStyle.PE_IndicatorArrowDown:
                  painter.setBrush(QColor("white"))
                  QProxyStyle.drawPrimitive(self, element, opt, painter, widget)
              else:
                  QProxyStyle.drawPrimitive(self, element, opt, painter, widget)
      
      


      But that doesn't work, I'm forgetting something?

      Thanks!

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 25 Feb 2017, 18:26 last edited by mrjj
      #2

      Hi

      QStyle.PE_IndicatorArrowDown:

      Are you sure that it draws any background and not just an image ?
      I would go and look at the original code to see what is being drawn.

      https://code.woboq.org/qt5/qtbase/src/widgets/styles/qcommonstyle.cpp.html
      Line 707

      Seems to be no brush involved :)

      G 1 Reply Last reply 26 Feb 2017, 01:35
      2
      • M mrjj
        25 Feb 2017, 18:26

        Hi

        QStyle.PE_IndicatorArrowDown:

        Are you sure that it draws any background and not just an image ?
        I would go and look at the original code to see what is being drawn.

        https://code.woboq.org/qt5/qtbase/src/widgets/styles/qcommonstyle.cpp.html
        Line 707

        Seems to be no brush involved :)

        G Offline
        G Offline
        gabodev
        wrote on 26 Feb 2017, 01:35 last edited by
        #3

        @mrjj Thank you very much! I have researched and been able to do what I was looking for. Here's what I did:

        def drawPrimitive(self, element, opt, painter, widget):
                if element == QStyle.PE_PanelButtonTool:
                    pressed = (opt.state & STATE_SUNKEN | opt.state & STATE_ON)
                    color = QColor("#323232")
                    if pressed:
                        color = QColor("#222222")
                    elif opt.state & STATE_ENABLED and opt.state & STATE_MOUSEOVER:
                        color = QColor("#4e4e4e")
                    painter.fillRect(opt.rect, color)
                elif element == QStyle.PE_IndicatorArrowDown or \
                        element == QStyle.PE_IndicatorArrowUp:
                    r = opt.rect
                    size = min(r.height(), r.width())
                    image = QImage(size, size, QImage.Format_ARGB32_Premultiplied)
                    image.fill(Qt.transparent)
                    image_painter = QPainter(image)
                    image_painter.setPen(QColor("#bdbfc0"))
                    image_painter.setBrush(QColor("#bdbfc0"))
                    polygon = QPolygon()
                    polygon.append(QPoint(0, r.width() * 0.5))
                    if element == QStyle.PE_IndicatorArrowDown:
                        polygon.append(QPoint(size, size * 0.6))
                        polygon.append(QPoint(size / 2, size * 0.1))
                    else:
                        polygon.append(QPoint(size, size * 0.5))
                        polygon.append(QPoint(size / 2, size * 0.9))
                    image_painter.drawPolygon(polygon)
                    image_painter.end()
                    pixmap = QPixmap.fromImage(image)
        
                    painter.drawPixmap(r.x() + (r.width() - size) / 2,
                                       r.y() + (r.height() - size) / 2, pixmap)
                else:
                    QProxyStyle.drawPrimitive(self, element, opt, painter, widget)
        

        Regards!

        Developer at Ninja-IDE

        1 Reply Last reply
        2

        3/3

        26 Feb 2017, 01:35

        • Login

        • Login or register to search.
        3 out of 3
        • First post
          3/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved