问题
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