Change QComboBox popup highlighted item background color
-
@qt-1234
Start out from:
https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qcombobox
https://stackoverflow.com/questions/43347419/how-to-change-the-color-of-non-selected-item-in-qcombobox-style-sheetitem:selected
&selection-background-color
are of interest. -
Hi! Setting stylesheet for the QComboBox doesnt work. Shown below is one of the recommendation shown in one of the link you have shared.
QComboBox QAbstractItemView { selection-background-color: rgb(255, 0, 0); selection-color: rgb(200, 200, 200); }
how do i go about using this? Do i use it as a stylesheet? or using setView? Selection-background-color refers to the highlighted color while selection-color refers to the font?
please advice thank you!
-
edit: Sorry for the typo, of course it's QComboBox instead of ComboBox.
Do this in your code:
my_combobox->setStylesheet("QComboBox { selection-background-color: red; }");
Obviously, change the "red" to whatever you desire. Also, have a look at this to get more inspiration on how to customize a QComboBox with stylesheets. It's very powerful, but the documentation is horrible for this stuff: https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qcombobox
-
@qt-1234
Not sure what you tried. If it was what @qwasder85 typed in above,ComboBox
will be wrong, you'll need to try withQComboBox ...
.I don't know exactly what you will want, you'll have to play and read. But I would start by verifying that from the docs page you can indeed get some of those samples to work, soi you know you're doing stylesheet right, and then move on to exactly what you need.
-
@qt-1234
It may not make any difference, but if you are setting this directly on aQComboBox
instance (I thought you mean for allQComboBox
es), you only need:ComboBox->setStylesheet("selection-background-color: rgb(0,255,0)");
However, there is some doubt over how this may have changed in various Qt versions. Also try instead:
ComboBox->setStylesheet("QComboBox::item:selected { background-color: rgb(0,255,0); }");
There are also suggestions in the links I quoted earlier which mention that you or may not need to put the attribute on
QComboBox QAbstractItemView
....One of these really should work!
-
Just to add my 2 cents to this conversation, I recently found this bugreport:
https://bugreports.qt.io/browse/QTBUG-77006That may or may not play a role here. I would suggest updating to 5.12.6 or 5.13.2, just in case 😉
-
I have a solution to this.
Linking signal currentIndexChanged() to a slot. So whenever there is a change of state in the index, the slot will be invoked. And in the slot do the following:
void MainWindow::indexChangeSlot() { QPalette p = comboBox->palette(); p.setColor(QPalette::Highlight, QColor(0,255,0)); comboBox->setPalette(p); p = comboBox->view()->palette(); p.setColor(QPalette::Highlight, QColor(0,255,0)); comboBox->view()->setPalette(p); }
-
@qt-1234 said in Change QComboBox popup highlighted item background color:
I have a solution to this.
Great, so time to call your post as solved? Please don't forget to mark it as such. Thanks