Version/Environment:
- Windows 10 64 bit
- Qt 5.11.0 MSVC2017 64 bit
I have a simple QComboBox
to enable/disable a feature:
QComboBox *onOffComboBox = new QComboBox();
onOffComboBox->insertItem(0, "Off");
onOffComboBox->insertItem(1, "On");
The combo box is added as a cell widget to a table:
this->ui->settingsTable->setCellWidget(rowNumber, 1, onOffComboBox);
Now i want to change the background color of the button but not the select items.
My first approach was simply to use QWidget
's setStyleSheet
function:
onOffComboBox->setStyleSheet("background-color: red;");
But this suppresses the standard style:
I also used variations with specific QComboBox
styles according to the documentation:
onOffComboBox->setStyleSheet("QComboBox::drop-down {background: red;}");
But this only colors the part with the arrow and suppresses it's style:
Using just QComboBox {background: red;}
has the same result as with background-color: red;
just the select items are not colored.
Just as described in this answer another approach is to use QPallete
:
QPalette pal = onOffComboBox->palette();
pal.setColor(QPalette::Base, QColor("red"));
onOffComboBox->setPalette(pal);
onOffComboBox->update(); // just in case this has any effect
This only colors the select items:
I also tried nearly all other QPalette
color roles:
QPalette::Window
, QPalette::Foreground
, QPalette::Button
- do nothing
QPalette::Base
- colors the select items (see pic)
QPalette::Text
- colors the text of the button and the select items
So, how can i change the color of the QComboBox
drop-down button background WITHOUT overwritting or suppressing the standard style?
The styles of the pop-up items also shouldn't change.
Here is an image of what i want:
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…