QWidget in QWidget overrides base widget stylesheet
-
I'm trying to make a child widget in a parent widget to have a different background, so I can know where it starts/ends, so I applied a stylsheet so that the background is different.
There's a problem though. The child widget overrides the parent widget stylesheet.One more thing I should mention. I added a stylesheet in the designer and that seemed to work. Then promoted the widget, because I needed to add a functionality to it, but then the stylesheet just did nothing. So I tried implementing the stylesheet in my code, but even the parent widget gets affected.
Here's what I have:
controller::controller(QWidget *parent) : QWidget(parent) { parent->setStyleSheet("background-color: blue;"); //code to set the child widget's background color }
This is how it looks in designer (don't mind the dark theme):
This is what I actually have:
How can I fix it?
-
I ended up fixing it like this.
I just edited it from the parent, because I have access to it from the .ui designer file.
Here's the code:QPalette back; back.setColor(QPalette::Window, QColor(0, 0, 0)); ui->area->setAutoFillBackground(true); ui->area->setPalette(back);
That code is being executed when you open it from mainwindow.
-
@Sucharek said in QWidget in QWidget overrides base widget stylesheet:
The child widget overrides the parent widget stylesheet.
It should not do that per se. Any stylesheet a child has will override anything it inherits from a parent, but it will not alter the parent.
I don't know where you have two widgets in what you show, what is parent, what is child. Have you put a layout on the parent onto which you add the child?
If your child area fills the whole of the parent's area then its color does not "override" that of the parent, but all you will see is the child's color as it is on top.
But your code reads:
parent->setStyleSheet("background-color: blue;"); //code to set the child widget's background color
That naturally does not set the child's stylesheet since you go
parent->setStyleSheet()
! So it does what you tell it to --- sets the parent's stylesheet/color. If you want to set the child's stylesheet then do so, don't make it set the parent's! -
Hi @JonB, alright I tried, but it doesn't do anything.
In the child widget initialization I added
controller::setStyleSheet("background-color: blue;");
That did not do anything. I also tried using
QPallete
, but that also doesn't work.Edit: Now it just looks normal
-
@Sucharek said in QWidget in QWidget overrides base widget stylesheet:
In the child widget initialization I added
controller::setStyleSheet("background-color: blue;");
I don't know what
controller
is, only you do. If it's the child you haven't said so.Anyway, there's not much to say. If the child is
this
thensetStyleSheet("background-color: blue;");
sets its background color to blue. And does not affect the parent. Which is what you asked for.Maybe your child widget is of minimal size inside its parent, so all you see is the parent. I don't know. Maybe you should try adding some other widget (let's say, a non-empty
QLabel
) onto your child widget to see where it is. -
I ended up fixing it like this.
I just edited it from the parent, because I have access to it from the .ui designer file.
Here's the code:QPalette back; back.setColor(QPalette::Window, QColor(0, 0, 0)); ui->area->setAutoFillBackground(true); ui->area->setPalette(back);
That code is being executed when you open it from mainwindow.
-