Solved How to properly draw QComboBox's view?
-
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?
-
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.
-
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.
-
@Qt_User72653
Hi
Can i ask what is the goal of it ?
other background color or completely new item drawing or ? -
- 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.
-
@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. -
Thanks, I'll take a look.