Qt:Unable to set Background color for qComboBox properly

688 Views Asked by At

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.the below

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

There are 1 best solutions below

13
On BEST ANSWER

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.

enter image description here

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

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