0

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.

adi
  • 984
  • 15
  • 33
  • Have you set the stylesheet in another part of your code? If you have, show it. – eyllanesc Aug 08 '18 at 12:17
  • Nope..I set the stylesheet here only.. – adi Aug 08 '18 at 14:34
  • So how have you established that your widgets have a black background? – eyllanesc Aug 08 '18 at 14:36
  • The parent widget of the layout inside which this combobox exists has a black background color – adi Aug 09 '18 at 01:26
  • What's the platform? And How did you set the background colour? – JustWe Aug 09 '18 at 03:09
  • I work in Ubuntu 16.04..and I have set the BG the same way as above to the parent widget – adi Aug 09 '18 at 03:12
  • @adi provide a [mcve] – eyllanesc Aug 09 '18 at 04:22
  • http://doc.qt.io/qt-5/qtwidgets-widgets-styles-example.html – Deep Aug 09 '18 at 04:22
  • Actually when I set the parent Widget 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,it is as shown above.I used the following for setting the parent widget BG: ui->centralWidget->setStyleSheet("background-image:url(./image.png)"); – adi Aug 09 '18 at 05:03
  • please edit the question to add code instead comment. – JustWe Aug 09 '18 at 05:07
  • @adi try with: `ui->centralWidget->setStyleSheet("QWidget#centralWidget{ border-image:url(./image.png)}");` – eyllanesc Aug 09 '18 at 05:23
  • @eyllanesc Thanks man..It works..Could u pls brief what is it so that I can understand and could u add this as answer so that I can upvote it. – adi Aug 09 '18 at 06:11

1 Answers1

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.

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)}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Could you tell me which is the better way to setBackground?stylesheets or QPalette?? What is the difference? – adi Aug 09 '18 at 06:45
  • @adi Could you mark it as correct? The author of the question, I mean, is the only one who can do it, if you do not know how to do it, check the [tour] – eyllanesc Aug 09 '18 at 06:46
  • oops Im not aware of it..Could u pls tell me which is the better way to setBackground?stylesheets or QPalette?? – adi Aug 09 '18 at 06:48
  • @adi ***What is the difference?***, are 2 ways to do it, each one has its advantage, for example qss is very similar to css (to css2.1 to be exact) so you could create a .qss file where you put all styles using selectors, etc. Or on the other hand use QPallete, not only is the only way, you could also use QStyle. – eyllanesc Aug 09 '18 at 06:49
  • But is that like no matter what approach we take for setting Background,all are inherited by the children widgets by default? – adi Aug 09 '18 at 07:03
  • @adi no, QPalette does not have the idea of ​​hierarchy – eyllanesc Aug 09 '18 at 07:04
  • So if my understanding is right:1.Use QPalette to a widget if its children doesn't want to inherit the BG 2.Use QStyle / StyleSheets to a widget if its children want to inherit the BG.Am I right? – adi Aug 09 '18 at 07:45
  • @adi with Qt Style Sheet you can also apply to a single widget but for this you must use the selectors, read http://doc.qt.io/qt-5/stylesheet-syntax.html – eyllanesc Aug 09 '18 at 07:48
  • In some cases,if I set BG using selector for the Parent Widget like u have specified,the combox box becomes white but the image disappears?What shall be done in that event? – adi Aug 09 '18 at 11:25
  • @adi I do not understand you, if you provide a [mcve] I could help you. – eyllanesc Aug 09 '18 at 11:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/177725/discussion-between-adi-and-eyllanesc). – adi Aug 09 '18 at 11:47
  • I have an issue reproduceable code.how shall I show you the code?Becoz the answer above works fine with the case posted!!so editing the question might not be gud..,but my issue reproducing code is with dynamic widgets . – adi Aug 09 '18 at 12:59
  • @adi then create another post – eyllanesc Aug 09 '18 at 12:59