Custom combo box look
-
Hi!
I want to make my custom delegate for one of the column of TableView. In this column user might choose Line Style(like Solid, Dot...) for each row. I have already implemented paint() event for delegate, so instead of integer numbers user see line itself. Now I want to implement setEditorData(), where I am going to use QComboBox as editor widget. The problem is that, when I set my custom delegate to QComboBox it effects only List but not PushButton. So in pup up list I see lines, and on PushButton I see just numbers 1,2,3...
My question is what is the easiest way to display particular line on PushButton? -
Hi,
Something's not clear, what PushButton ?
-
@SGaist QComboBox consist of button and a list. This is mentioned in documentation http://doc.qt.io/qt-5/qcombobox.html#details There is an underlying QStandardItemModel and QListView used in QComboBox. I have implemented my own delegate, which paint different style lines instead of integer numbers. The problem is that when I set this delegate to QComboBox the appearance of QListView is changing but button still display integer number instead of line.
Fore example in Color Editor Factory Example http://doc.qt.io/qt-5/qtwidgets-itemviews-coloreditorfactory-example.html
there is a list of colors witch looks like square of particular color, and the name of color. But on the top of the list (widget witch looks like a button with an arrow), there is only name. So my question is about changing appearance of this button. -
The delegate only paints items in the popup list.
The button part is styled depending on the platform. If you want custom painting of the widget itself then subclass QComboBox and overridepaintEvent
. -
The delegate only paints items in the popup list.
The button part is styled depending on the platform. If you want custom painting of the widget itself then subclass QComboBox and overridepaintEvent
.@Chris-Kawa It means that I will have to write all paint routine on very low level from scratch, no other way? I hoped that there was a way just to add a little code and paint a line on the button.
-
@Chris-Kawa It means that I will have to write all paint routine on very low level from scratch, no other way? I hoped that there was a way just to add a little code and paint a line on the button.
@Harb you can overwrite the
paint()
as @Chris-Kawa suggested, then first call the standard implementation in there usingQComboBox::paint()
and then you can draw your line over the standard button that was rendered for you.Something like:
MyComboBox::paint(...) { // Render the default combo box QComboBox::paint(...); // Draw your stuff over it painter->drawLine(...); }
-
@Harb you can overwrite the
paint()
as @Chris-Kawa suggested, then first call the standard implementation in there usingQComboBox::paint()
and then you can draw your line over the standard button that was rendered for you.Something like:
MyComboBox::paint(...) { // Render the default combo box QComboBox::paint(...); // Draw your stuff over it painter->drawLine(...); }
@Joel-Bodenmann Wow, tnx! Yes, it will work!
-
Hi,
may be it will work, but when a QComboBox it is used as an editor in a table view item delegate things work other way.
Do you understand that because the push button/text edit overlays the table view item's surface then the appearance of both are the same and the painting could simply be done by the table view item delegate? Moreover the QComboBox could insert icons( pixmaps ) in the list so there might be no need of subclassing? ... the discussion started about a table view and a QComboBox editor, all together, then switched to the isolated case of a custom QComboBox, which is not the same. Again, because the table view item delegate will do the final painting, the editor will be closed and destroyed, there could be some mismatch between the two paintings ... -
@NicuPopescu True. In that case the custom overpainting can be done in some static method of the subclassed combo and then called from both the
paintEvent
of the combo andpaint
of the delegate.