How can I set the backgroud color of a QComboBox in a dropdown state?
I can change the color QComboBox
as follows:
QPalette palette = ui->selectSource->palette(); palette.setColor(QPalette::Active, QPalette::Button, Qt::white); palette.setColor(QPalette::Inactive, QPalette::Button, Qt::white); ui->selectSource->setPalette(palette);
It turns white, but when it is in the dropdown state, it still has a gray color (default).
How can this be changed?
You can also apply another palette to the combo box dropdown menu. To get a pointer to the dropdown you can use the function QComboBox::view()
. So your code will look like this:
QPalette palette = ui->selectSource->palette(); palette.setColor(QPalette::Active, QPalette::Button, Qt::white); palette.setColor(QPalette::Inactive, QPalette::Button, Qt::white); QPalette view_palette = ui->selectSource->view()->palette(); view_palette.setColor(QPalette::Active, QPalette::Background, Qt::white); ui->selectSource->view()->setPalette(view_palette);
source to share
You must also set the role QPalette::Base
. Quoting from the Qt documentation about QPalette::Base
:
Used primarily as a background color for text input widgets, but can also be used for other coloration - for example, combobox background for comboboxes for dropdowns and toolbar grips. This is usually white or some other light color.
So, you should also have:
palette.setColor(QPalette::Base, Qt::white);
source to share