Unsolved qss and children objects
-
In Designer I have created QWidget with child QPushButton within that have names widget and btn accordingly. At widget I have set up qss:
#widget { background-color: red; } #widget:hover { background-color: green; } #widget:hover > #btn { background-color: rgb(255, 0, 0); } #widget:!hover > #btn { background-color: rgb(0, 0, 255); }
But color changes only at widget. Why? How its syntax is correct?
-
Hi,
Your error description is not clear. Do you mean your stylesheet only applies to your widget named widget ?
-
@SGaist
Hi,
yes, I thought QPushButton should change the self background color too depending on the hover state of widget. -
There's no reason for that. From what you wrote, you applied that stylesheet on "widget".
-
@SGaist
But btn is child of widget, and hence syntax > #btn is mean third and fourth rules applies to btn. Or I something missed? -
Please show your code, I have misunderstood your description.
-
QWidget* widget = new QWidget; widget->setObjectName("widget"); QPushButton* btn = new QPushButton(widget); btn->setObjectName("btn"); widget->setStyleSheet("...see above...");
-
Which version of Qt ?
On which platform ? -
@SGaist
Qt 5.15.1
Windows 10 (amd64) -
@magrif said in qss and children objects:
#widget:hover > #btn { background-color: rgb(255, 0, 0); }
Did you try
QWidget:hover > QPushButton { background-color: rgb(255, 0, 0); }
just in case?
And I would start out by testing changing the button without specifying
hover
to eliminate that from being the issue.#widget > #btn { ... /* or */ QWidget > QPushButton { ...
BTW, according to https://doc.qt.io/qt-5/stylesheet-syntax.html#selector-types I do not see that
#widget
is a legal selector in QSS? It only shows acceptingQWidget#widget
? EDIT Oh you said in Designer, maybe it gets translated from what you show? Bear that in mind for my suggestions above. -
@JonB said in qss and children objects:
Did you try
Hi
Same results and withQWidget:hover > QPushButton { ...
and with
QWidget#widget:hover > QPushButton#btn { ...
:(