How to exclude a specific widget from the global stylesheet?
-
Hi,
In my application I set a global stylesheet, for example:
qApp->setStyleSheet("QPushButton { background-color: red; }");
This works fine, but now I want one specific QPushButton not to use the global style and instead use the default system look.
I tried setting a different stylesheet on that button, but it still inherits the global rules.
Question:
How can I clear or override the global stylesheet for a single widget, so that it is rendered with the default system style instead of the global QSS?Thanks!
-
Hi,
In my application I set a global stylesheet, for example:
qApp->setStyleSheet("QPushButton { background-color: red; }");
This works fine, but now I want one specific QPushButton not to use the global style and instead use the default system look.
I tried setting a different stylesheet on that button, but it still inherits the global rules.
Question:
How can I clear or override the global stylesheet for a single widget, so that it is rendered with the default system style instead of the global QSS?Thanks!
@Qt123 said in How to exclude a specific widget from the global stylesheet?:
I tried setting a different stylesheet on that button, but it still inherits the global rules.
That should work. How do you do it?
-
TEMPLATE = app TARGET = test INCLUDEPATH += . QT+= widgets SOURCES += main.cpp#include <QApplication> #include <QWidget> #include <QPushButton> #include <QVBoxLayout> class BoringButton: public QPushButton { Q_OBJECT public: explicit BoringButton(const QString &text, QWidget *parent = nullptr): QPushButton(text, parent) { } }; int main(int argc, char **argv) { QApplication app(argc, argv); QWidget widget; QString originalStyleSheet = widget.styleSheet(); widget.setStyleSheet( ".QPushButton { background-color: cyan; } "); QVBoxLayout *layout = new QVBoxLayout(&widget); QPushButton *button1 = new QPushButton("Styled"); layout->addWidget(button1); QPushButton *button2 = new BoringButton("Default"); layout->addWidget(button2); widget.show(); return app.exec(); } #include "main.moc"
-
You can see if any of the selectors (https://doc.qt.io/qt-6/stylesheet-syntax.html) work for you. You could create your own subclass of
QPushButton. As long as all the other buttons are actualQPushButtons the class selector.QPushButtonwould only style these, but not your subclass. Or you could try to use a Property Selector with your own property. But I'm not sure if that works for buttons that don't have that property. (You see, it is a lot easier to just specify something for a specific button, but a lot harder to specify something for all other buttons.)