39

I am trying to set the background color for a double spin box, and I am not sure what function I should use.

I saw some function called SetBackgroundRole which accepts a Qt::ColorRole, but I am not sure how to use this one as well.

Kindly let me know, what's the simple way to change the background color of a QComboBox or QDoubleSpinBox?

demonplus
  • 5,613
  • 12
  • 49
  • 68
AMM
  • 17,130
  • 24
  • 65
  • 77

9 Answers9

46

fhe is generally correct, but doesn't account for the widgets (like spin boxes and buttons/comboboxes) that use a different background role in the palette. A more general solution would be something like this:

QPalette pal = widget.palette();
pal.setColor(widget.backgroundRole(), Qt::blue);
widget.setPalette(pal);

Alternatively, you could look into the descriptions of the various palette roles and figure out the one you want, then apply it to the widget containing the others you want changed. The palette changes should propagate to the children widgets.

Caleb Huitt - cjhuitt
  • 14,785
  • 3
  • 42
  • 49
42

Using a QPalette isn't guaranteed to work for all styles, because style authors are restricted by the different platforms' guidelines and by the native theme engine.

To make sure your background color will be correct, I would suggest to use the Qt Style Sheet. Here is what I did to change the background color of a QComboBox:

myComboBox->setStyleSheet("QComboBox { background-color: blue; }");

I haven't specifically tried for a QSpinBox, but I guess it'll work the same !

Tarod
  • 6,732
  • 5
  • 44
  • 50
Jérôme
  • 26,567
  • 29
  • 98
  • 120
  • 8
    Using a stylesheet for setting the background colour isn't the correct approach, unless you're willing to also style the other sub-controls in the QComboBox (i.e. drop-down and down-arrow). See the note from the Qt documentation: "With complex widgets such as QComboBox and QScrollBar, if one property or sub-control is customized, all the other properties or sub-controls must be customized as well." Link: http://doc.trolltech.com/4.5/stylesheet-customizing.html – Krsna Dec 18 '09 at 22:56
  • i get white borders at the top and the bottom where the rounded edges are on mac – tofutim Jun 18 '18 at 23:17
14

Actually, if you look at the Qt docs for QPalette in the case of a QComboBox the background role is probably not what you want. What you want is:

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 here is the code I am using to set the background color of a combo box I am using to match the color of the widget it is on:

QPalette pal = myComboBox->palette();
pal.setColor(QPalette::Base, pal.color(QPalette::Window));
myComboBox->setPalette(pal);
Corwin Joy
  • 645
  • 7
  • 14
11

Apparently in Qt 4.1 and onwards, you need to set this->setAutoFillBackground( true ); for the palette to apply the background color.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shake
  • 1,752
  • 4
  • 18
  • 22
4

While the previous answers may set the background color for a non-editable QComboBox, they do not work for an editable QComboBox. For that case, you need to derive the QLineEdit widget used for the editing and reset its background.

Here is how I got it to work:

    QComboBox *myComboBox = new QComboBox();
    myComboBox->setEditable(true);
    QColor backColor = QColor(246, 230, 230);
    QLineEdit *lineEditor = myComboBox->lineEdit();
    QPalette pal = lineEditor->palette();
    pal.setColor(QPalette::Base, backColor);
    lineEditor->setPalette(pal);
Tarod
  • 6,732
  • 5
  • 44
  • 50
Dan Blanks
  • 41
  • 1
3

Construct a palette that is blue no matter what the actual widget:

comboBox->setPalette( QPalette( Qt::blue ) );
Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
1

No previously answers worked for me, but I made a mix of all responses and finally worked on Qt 5.12:

QPalette pal = ui.widget->palette();
pal.setColor(QPalette::Base, Qt::red);
ui.widget->setPalette(pal);
Patapoom
  • 794
  • 1
  • 7
  • 16
  • Nice answer! Another cool, and more subtle, highlight effect is to use `QPalette::Background` instead of `QPalette::Base`, but with dark colors like magenta or green. – psimpson Sep 18 '19 at 21:00
0

I'd try something like

QPalette pal = widget.palette();
pal.setColor(QPalette::Window, Qt::blue);
widget.setPalette(pal);
fhe
  • 6,099
  • 1
  • 41
  • 44
  • 1
    I tried doing this, but somehow the background color is not getting updated. Any idea what could be the reason. Is there anything else you had assumed.. – AMM Oct 07 '08 at 10:28
  • What i mean is should i be invoking something like a repaint or anything after doing the setPalette – AMM Oct 07 '08 at 10:33
  • I have no QT to test here, but QWidget::update() should force a repaint. Have you tried Jérôme's solution? – fhe Oct 08 '08 at 08:55
  • `QPalette::Window` is not the `backgroundRole()` of `QComboBox`, so this code does nothing for a `QComboBox`. – Marc Mutz - mmutz Jul 26 '09 at 11:03
-1
comboBox->setPalette( QPalette( Qt::blue ) );

Works fine for me!

Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
  • 1
    I tried this, but mine only works if the QSpinBox is disabled (i.e. if the QSpinBox is enaled, the color back to normal). Any idea? – ismailsunni Jan 03 '18 at 07:25