Unsolved How to propagate/limit stylesheets through code
-
I have a main stylesheet file (qss) with styles for various controls (QDialog, QLabel, QEdit etc) defined all in one file. In my program I need to show certain windows with this stylesheet and others without (e.g. show native file dialog, messagebox etc with system appearance).
Noticed following odd behavior:
-
If I copy/paste style to each Dialog's stylesheet property in Qt Designer, then all controls/widgets update correctly as well, implying that child widgets are getting stylesheet from parent. The downside is that I have to copy stylesheet and update each dialog at design time - so not an option.
-
If I programmatically read the qss file into QString and then apply that to dialog via setStylesheet property, then only dialog's appearance seems to change, all the controls/widgets on dialog still have default appearance. This is odd, why does it work in Qt Designer though?
-
If I use QApplication:setStylesheet() method, then all dialogs and child controls have correct appearance. However, now even the windows I don't want styled have stylesheet applied.
I'd like to go with option 2, but why isn't it working? Also, even when it works, I've noticed that all child widgets (e.g. QMessageBox) get stylesheet applied even if I set QMessageBox::setStyleSheet("")?
-
-
@Taytoo
For option 2: Qt Designer is not magic, look at the generatedui....h
file and compare what it's generating against how you're doing it.For option 3, one way is you could be more selective in your stylesheets selectors. Don't forget e.g.
#SpecificDialog QPushButton
only applies to buttons on that named dialog. There are classes too.
-
@JonB said in How to propagate/limit stylesheets through code:
@Taytoo
For option 2: Qt Designer is not magic, look at the generatedui....h
file and compare what it's generating against how you're doing it.It's not doing anything special, just setting stylesheet for QDialog and nowhere else.
For option 3, one way is you could be more selective in your stylesheets selectors. Don't forget e.g.
#SpecificDialog QPushButton
only applies to buttons on that named dialog. There are classes too.
Defining custom classes gets too tricky e.g. with QStackedWidget and QTabWidget, I tried specifying a custom class but for some reason the style just wasn't applying
There has to be a reason why setstylesheet works for all widgets at QDialog level when set in Designer but fails when doing it in code?
-
@Taytoo said in How to propagate/limit stylesheets through code:
It's not doing anything special, just setting stylesheet for QDialog and nowhere else.
Then in principle if it works from Designer-generated code you should be able to make it work in manual code?
Defining custom classes gets too tricky e.g. with QStackedWidget and QTabWidget, I tried specifying a custom class but for some reason the style just wasn't applying
I can only say that I do not find this. I do use custom classes when necessary, and it works for me. If I want to apply stylesheet rules across a variety of elements, I use the "dynamic class" principle described at https://doc.qt.io/Qt-5/stylesheet-syntax.html#selector-types Property Selector & https://doc.qt.io/Qt-5/stylesheet-examples.html#customizing-using-dynamic-properties. I use this against a dynamic
class
property, so I can have things like:/* stylesheet */ .backgroundColorTransparent { background-color: transparent; } /* code */ widget.setProperty("class", "backgroundColorTransparent");
I can't recall now, but I think you can associate multiple classes with a widget as a space-separated list on the
class
property.One other thought: if your only problem is with *background color", have a read of https://forum.qt.io/topic/115640/how-to-see-the-area-detected-by-qmouseevent/9.