How to prevent Qt child windows from inheriting the style of the parent window?
-
For example, there are two windows named
ParentWidget
andChildWidget
, both of which have a QPushButton control with the default namepushButton
.
Now, create a widget inParentWidget
, and promote it toChildWidget
. Then I set the following style sheet for the outermost window inParentWidget
:#ParentWidget QPushButton#pushButton{ background:#1785E6; }
After the program runs, this style sheet will affect both the button in
ParentWidget
and the button inChildWidget
. How can I make it only affect the controls in ParentWidget? -
For example, there are two windows named
ParentWidget
andChildWidget
, both of which have a QPushButton control with the default namepushButton
.
Now, create a widget inParentWidget
, and promote it toChildWidget
. Then I set the following style sheet for the outermost window inParentWidget
:#ParentWidget QPushButton#pushButton{ background:#1785E6; }
After the program runs, this style sheet will affect both the button in
ParentWidget
and the button inChildWidget
. How can I make it only affect the controls in ParentWidget?@Aizzzz said in How to prevent Qt child windows from inheriting the style of the parent window?:
How can I make it only affect the controls in ParentWidget?
ParentWidget > QPushButton { ... }
This will only affect
QPushButtons
which are direct children ofParentWidget
. -
Add the
property
parentName to all yourQPushButton
elements inparentWidget
, and set the value to "parentWidget" for each of them.QObjectList buttons = ParentWidget->findChildren<QPushButton*>(); for (QPushButton* button : buttons) { button->setProperty("parentName", "parentWidget"); }
in qss:
QPushButton[parentName = "parentWidget"] { background: #123456; }