How do I set the background color of a widget like combobox or double spin box?

瘦欲@ 提交于 2019-11-27 03:43:52

问题


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?


回答1:


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 !




回答2:


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.




回答3:


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



回答4:


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




回答5:


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



回答6:


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

comboBox->setPalette( QPalette( Qt::blue ) );



回答7:


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



回答8:


I'd try something like

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



回答9:


comboBox->setPalette( QPalette( Qt::blue ) );

Works fine for me!



来源:https://stackoverflow.com/questions/177778/how-do-i-set-the-background-color-of-a-widget-like-combobox-or-double-spin-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!