Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to exclude a specific widget from the global stylesheet?
Qt 6.12 LTS is out! See what's new in the release blog

How to exclude a specific widget from the global stylesheet?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 5 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt123
    wrote on last edited by
    #1

    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!

    jsulmJ 1 Reply Last reply
    0
    • Q Qt123

      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!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by ChrisW67
        #3
        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"
        

        99a1ccb3-be73-4e77-bafa-f2d1dd63178d-image.png

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bonnie
          wrote on last edited by
          #4

          You can't "un-inherit" stylesheet. You can only overrride it with other value of the same property.

          1 Reply Last reply
          1
          • S Offline
            S Offline
            SimonSchroeder
            wrote on last edited by
            #5

            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 actual QPushButtons the class selector .QPushButton would 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.)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt
            • Unsolved