Search code examples
c++qtbackgroundqcombobox

How to set QComboBox background color on drop down state?


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?


Solution

  • 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);