Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use 2 stylesheets in one system?
Qt 6.11 is out! See what's new in the release blog

How to use 2 stylesheets in one system?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 771 Views 3 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.
  • T Offline
    T Offline
    Thinium
    wrote on last edited by Thinium
    #1

    My system has a generic stylesheet that applies to most parts of the system. But there is another stylesheet that should be applied to one specific QMainWindow. I tried to first set the main style after the app is created and then apply the specific stylesheet. But the minor style sheet isn't applied to the window. Is there something wrong in this approach? Thanks!

    in my Main.cpp

    int main(int argc, char *argv[])
    {
           QApplication app(argc, argv);
           // apply the major style sheet to the whole app
           app.setStyle(new MainStyleSheet);
    
           // for a specific window, use another style sheet
           AnotherWindow* window = AnotherWindow::instance();
           window->setStyle(new MinorStyle);
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You are talking about style sheets however, it looks like you implemented two custom styles.
      What is the exact situation ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply
      3
      • SGaistS SGaist

        Hi,

        You are talking about style sheets however, it looks like you implemented two custom styles.
        What is the exact situation ?

        T Offline
        T Offline
        Thinium
        wrote on last edited by
        #3

        @SGaist thanks for the reply! My system has multiple windows. Most windows should be using stylesheet1, but one window should use stylesheet2. I can't apply both stylesheets to the app (by calling app.setStyle) as one will be overwritten by the other. I tried to apply stylesheet1 to the whole app first, and then apply stylesheet2 to one window (as shown in the previous code example). But only stylesheet1 is actually used. In the specific window the stylesheet2 isn't used at all.

        mrjjM 1 Reply Last reply
        0
        • T Thinium

          @SGaist thanks for the reply! My system has multiple windows. Most windows should be using stylesheet1, but one window should use stylesheet2. I can't apply both stylesheets to the app (by calling app.setStyle) as one will be overwritten by the other. I tried to apply stylesheet1 to the whole app first, and then apply stylesheet2 to one window (as shown in the previous code example). But only stylesheet1 is actually used. In the specific window the stylesheet2 isn't used at all.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @Thinium
          Hi
          You do mean stylesheet right?
          as you show setStyle which is used by QStyle objects and is not related to a stylesheet as such.

          Stylesheet
          https://doc.qt.io/qt-5/stylesheet-reference.html
          QStyle
          https://doc.qt.io/qt-5/qstyle.html

          1 Reply Last reply
          2
          • T Offline
            T Offline
            Thinium
            wrote on last edited by Thinium
            #5

            ah, sorry. I actually meant style. Here are the two styles:

            // used by most parts of the system
            class MainStyle : public QProxyStyle
            {
              public :
              void polish(QWidget *w) {w->setAttribute(Qt::WA_MacSmallSize);}
            };
            
            // used only by one window
            class MinorStyle: public QProxyStyle
            {
             public:
             void polish(QPalette &palette)
             {
                  palette.setColor(QPalette::Window, QColor(53, 53, 53));
                  palette.setColor(QPalette::WindowText, Qt::white);
                  palette.setColor(QPalette::Disabled, QPalette::WindowText,
                                   QColor(127, 127, 127));
                  palette.setColor(QPalette::Base, QColor(42, 42, 42));
                  palette.setColor(QPalette::AlternateBase, QColor(66, 66, 66));
                  palette.setColor(QPalette::ToolTipBase, Qt::white);
                  palette.setColor(QPalette::ToolTipText, QColor(53, 53, 53));
                  palette.setColor(QPalette::Text, Qt::white);
                  palette.setColor(QPalette::Disabled, QPalette::Text, QColor(127, 127, 127));
                  palette.setColor(QPalette::Dark, QColor(35, 35, 35));
                  palette.setColor(QPalette::Shadow, QColor(20, 20, 20));
            
                  // there seems to be a bug for the QPushButton background color
                  // https://bugreports.qt.io/browse/QTBUG-35296
                  palette.setColor(QPalette::Button, Qt::gray);
                  palette.setColor(QPalette::ButtonText, Qt::white);
            
                  palette.setColor(QPalette::Disabled, QPalette::ButtonText,
                                   QColor(127, 127, 127));
                  palette.setColor(QPalette::BrightText, Qt::white);
                  palette.setColor(QPalette::Link, QColor(42, 130, 218));
                  palette.setColor(QPalette::Highlight, QColor(42, 130, 218));
                  palette.setColor(QPalette::Disabled, QPalette::Highlight, QColor(80, 80, 80));
                  palette.setColor(QPalette::HighlightedText, Qt::white);
                  palette.setColor(QPalette::Disabled, QPalette::HighlightedText,
                                   QColor(127, 127, 127));
             }
            };
            
            //in my main.cpp
            int main(int argc, char *argv[])
            {
                   QApplication app(argc, argv);
                   // apply the major style sheet to the whole app
                   app.setStyle(new MainStyle);
            
                   // for a specific window, use another style sheet
                   AnotherWindow* window = AnotherWindow::instance();
                   window->setStyle(new MinorStyle);
            }
            
            

            The Minor style isn't applied to AnotherWindow. I read the document of QWidget:
            https://doc.qt.io/qt-5/qwidget.html#setStyle

            It says:
            Setting a widget's style has no effect on existing or future child widgets.

            I wonder if this is the reason why I don't see the style applied correctly to the child widgets of the AnotherWindow. If that's the case, is there a way to apply the style exclusively to AnotherWindow and its child widgets?

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi
              You have to set the same style on its children too.
              You can use findChildren to make it easy.

              1 Reply Last reply
              1

              • Login

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