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. How to properly draw QComboBox's view?

How to properly draw QComboBox's view?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.2k Views 2 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.
  • Q Offline
    Q Offline
    Qt_User72653
    wrote on last edited by
    #1

    I subclassed QComboBox and have overridden the paintEvent() of it, but when I filter the paintEvent of the view it's still drawing the default style.

      if(event->type() == QEvent::Paint && object == view()) {
        auto e = static_cast<QPaintEvent*>(event);
        auto painter = QPainter(view());
        painter.fillRect(e->rect(), Qt::black);
        painter.fillRect(1, 1, view()->width() - 2, view()->height() - 2,
          Qt::white);
        return true;
      }
    

    With this code I expected to see a white view with a black border and no items in the list. But, I get the default view drawn with a black border on the bottom and right edges.

    How to I prevent the view from being drawn? Shouldn't returning true from the filter prevent the default view style from being drawn?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      If you want as custom view, it would be simpler to create your own QAbstractItemView or QListView and call setView on your combo box.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Q 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        If you want as custom view, it would be simpler to create your own QAbstractItemView or QListView and call setView on your combo box.

        Q Offline
        Q Offline
        Qt_User72653
        wrote on last edited by
        #3

        @SGaist

        Thanks.

        I was hoping to avoid having to create a new view for it, but if that's easier I'll give that a go.

        mrjjM 1 Reply Last reply
        0
        • Q Qt_User72653

          @SGaist

          Thanks.

          I was hoping to avoid having to create a new view for it, but if that's easier I'll give that a go.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Qt_User72653
          Hi
          Can i ask what is the goal of it ?
          other background color or completely new item drawing or ?

          Q 1 Reply Last reply
          0
          • mrjjM mrjj

            @Qt_User72653
            Hi
            Can i ask what is the goal of it ?
            other background color or completely new item drawing or ?

            Q Offline
            Q Offline
            Qt_User72653
            wrote on last edited by
            #5

            @mrjj

            • New background color for hovered item
            • Padding for hovered item
            • Custom border color for top, with a different color for bottom and sides
            • Remove the outline for the hovered item
            • Custom size for list items

            That's just off the top of my head, there's a few more things I'm forgetting. Some of this works with CSS, some works with quirks, and some I can't get to work at all, so, I figured I would just draw it myself rather than try to deal with the issues with CSS.

            I originally considered making a custom view for it, but I didn't want to have to deal with managing items in the list; I only wanted a custom style.

            mrjjM 1 Reply Last reply
            0
            • Q Qt_User72653

              @mrjj

              • New background color for hovered item
              • Padding for hovered item
              • Custom border color for top, with a different color for bottom and sides
              • Remove the outline for the hovered item
              • Custom size for list items

              That's just off the top of my head, there's a few more things I'm forgetting. Some of this works with CSS, some works with quirks, and some I can't get to work at all, so, I figured I would just draw it myself rather than try to deal with the issues with CSS.

              I originally considered making a custom view for it, but I didn't want to have to deal with managing items in the list; I only wanted a custom style.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Qt_User72653
              Ah, its plenty.
              Well yes, a custom view should be more optimal then.
              You can use
              https://code.woboq.org/qt5/qtbase/src/widgets/itemviews/qlistview.cpp.html
              to have a look of handling states etc.

              Q 1 Reply Last reply
              0
              • mrjjM mrjj

                @Qt_User72653
                Ah, its plenty.
                Well yes, a custom view should be more optimal then.
                You can use
                https://code.woboq.org/qt5/qtbase/src/widgets/itemviews/qlistview.cpp.html
                to have a look of handling states etc.

                Q Offline
                Q Offline
                Qt_User72653
                wrote on last edited by
                #7

                @mrjj

                Thanks, I'll take a look.

                1 Reply Last reply
                0

                • Login

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