Qt:Unable to set Background color for qComboBox properly

时光毁灭记忆、已成空白 提交于 2019-12-11 16:55:38

问题


I have a QCombobox and I want to set a white background color.This is my code.

QComboBox *cBox = new QComboBox;
cBox->addItem("Text1");
cBox->setStyleSheet("background-color:white");

This combobox has a parent widget whose background is an image and is set as given below:

ui->centralWidget->setStyleSheet("border-image:url(./image.png)"); 

When I set the parent Widget[centralWidget] background as some other color,then the white BG works properly for the combobox.But when I set an image as the parent Widget background,the UI looks like this.

In the above pic,the black Bg is an image.Could someone highlight me what am I missing.Any help will be really helpful.


回答1:


When you do not indicate to which widget you are going to apply some property, they will be applied to all your children, for this reason the same QComboBox background image is applied to the child of centralWidget.

In your case you want to apply only to the centralWidget, and by default Qt Designer uses the same name for the name of the variable that represents the widget and the objectName.

So if you want to apply to a widget we can use the objectName as selector:

QWidget#centralWidget{ border-image:url(./image.png)}


来源:https://stackoverflow.com/questions/51742821/qtunable-to-set-background-color-for-qcombobox-properly

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