3

I can change QComboBox color like this:

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 becomes white, but when its in drop down state it still have some gray color (default).

How to change this?

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

0

You should also set QPalette::Base role. Quote from the Qt documentation about QPalette::Base :

Used mostly as the background color for text entry widgets, but can also be used for other painting - such as the background of combobox drop down lists and toolbar handles. It is usually white or another light color.

So you should also have :

palette.setColor(QPalette::Base, Qt::white);
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • 1
    That's weird. It works for me both on Linux and Windows. – Nejat Dec 09 '14 at 08:40
  • @Tay2510, now that's weird for me, cause "palette.setColor(QPalette::Base, Qt::white);" do nothing in my code. Maybe some other property of my main window is different or I miss something. –  Dec 09 '14 at 09:00
0

You could apply one more palette to the combo box's drop down view too. To get the pointer to the drop down view you can use the QComboBox::view() function. So, your code will look like:

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);
vahancho
  • 20,808
  • 3
  • 47
  • 55