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. Set stylesheet to parent frame only
Qt 6.11 is out! See what's new in the release blog

Set stylesheet to parent frame only

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 330 Views 2 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.
  • A Offline
    A Offline
    angela2016
    wrote on last edited by
    #1

    Hi everybody,

    I'm trying to set a background color to a frame. That frame has a sub frame with a label in it. These two should have the default stylesheet/color. But I can't for the life of me figure it out. Here are some observations:

    1. Trying to set the stylesheet to only objects of a certain object name via # doesn't work if such object doesn't exist (as expected).

    2. But: trying to set the stylesheet to only objects of a certain object name via # doesn't prevent subwidgets from inheriting the stylesheet. Even if they have an object name of their own.

    3. This is true even if the class isn't really the same. I tried making a subclass for the main frame ("AFrame"), that inherits from QFrame, but left the subframe as QFrame proper. The QFrame and QLabel still took on the AFrame's stylesheet.

    4. This isn't true in the other direction, though. The QFrame subwidget accepts the parent's AFrame stylesheet, but a stylesheet set directly with AFrame doesn't work (as expected).

    5. I can't find a way to delete the stylesheet of the subwidgets. I can set a different color, but I can't put them back to the default color.

      
      AFrame *vMainFrame = new AFrame(this);
      vMainFrame->setObjectName("AFrame");
      QHBoxLayout *vLayoutMainFrame = new QHBoxLayout(vMainFrame);
      ui->verticalLayout_2->addWidget(vMainFrame);
      
      QFrame *vSubFrame = new QFrame(this);
      vSubFrame->setObjectName("SOMEOTHERFRAME");
      vLayoutMainFrame->addWidget(vSubFrame);
      
      QHBoxLayout *vLayoutSubFrame = new QHBoxLayout(vSubFrame);
      QLabel* vLabel = new QLabel("womimwolmiplomw",this);
      vLabel->setObjectName("somelabel");
      vLayoutSubFrame->addWidget(vLabel);
      
      
      //main no color, sub frame no color, label is green
      //vMainFrame->setStyleSheet(QString("QFrame#somelabel {background-color: green}"));
      
      //main no color, sub frame + label green
      vMainFrame->setStyleSheet(QString("QFrame#SOMEOTHERFRAME {background-color: green}"));
      
      //no color
      //vMainFrame->setStyleSheet(QString("AFrame#SOMEOTHERFRAME {background-color: green}"));
      
      //main is blue, sub frame + label green
      //vMainFrame->setStyleSheet(QString("QFrame#AFrame {background-color: blue} QFrame#SOMEOTHERFRAME {background-color: green}"));
      
      //main is blue, sub frame + label blue
      //vMainFrame->setStyleSheet(QString("AFrame#AFrame {background-color: blue} QFrame#SOMEOTHERFRAME {}"));
      //vMainFrame->setStyleSheet(QString("QFrame#AFrame {background-color: blue} QFrame#SOMEOTHERFRAME {background-color: }"));
      //vMainFrame->setStyleSheet(QString("QFrame#AFrame {background-color: blue} QFrame#SOMEOTHERFRAME {}"));
      
      //no color
      //vMainFrame->setStyleSheet(QString("AFrame#AFrameNONEXISTING {background-color: blue} QFrame#SOMEOTHERFRAME {}"));
      //vMainFrame->setStyleSheet(QString("QFrame#AFrameNONEXISTING {background-color: blue} QFrame {}"));
      
      //no color for main, sub frame + label green
      //vMainFrame->setStyleSheet(QString("QFrame#AFrameNONEXISTING {background-color: blue} QFrame#SOMEOTHERFRAME {background-color: green}"));
      
      
        
    
    Best wishes,
    Angela
    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      If you want to change the background of only two widget, going the palette way would be easier:

      QPalette *palette = vMainFrame->palette();
      palette.setColor(QPalette.Window, QColor("green"));
      vMainFrame->setPalette(palette);
      

      And the same for your other widget.

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

      A S 2 Replies Last reply
      0
      • SGaistS SGaist

        Hi,

        If you want to change the background of only two widget, going the palette way would be easier:

        QPalette *palette = vMainFrame->palette();
        palette.setColor(QPalette.Window, QColor("green"));
        vMainFrame->setPalette(palette);
        

        And the same for your other widget.

        A Offline
        A Offline
        angela2016
        wrote on last edited by
        #3

        @SGaist Thanks for the hint. I eventually got it working with:

            QPalette palette = vMainFrame->palette();
            palette.setColor(QPalette::Window, QColor("blue"));
            vMainFrame->setAutoFillBackground( true );
            vMainFrame->setPalette(palette);
        
            QPalette p;
            QColor c = p.color(QPalette::Window);
            p.setColor(QPalette::Window, c);
            vSubFrame->setAutoFillBackground( true );
            vSubFrame->setPalette(p);
        

        It should be said that I found the second part on the old Qt forum and another user was apparently horrified by it, but it's the only way of resetting the palette I found that actually worked.

        1 Reply Last reply
        0
        • A angela2016

          Hi everybody,

          I'm trying to set a background color to a frame. That frame has a sub frame with a label in it. These two should have the default stylesheet/color. But I can't for the life of me figure it out. Here are some observations:

          1. Trying to set the stylesheet to only objects of a certain object name via # doesn't work if such object doesn't exist (as expected).

          2. But: trying to set the stylesheet to only objects of a certain object name via # doesn't prevent subwidgets from inheriting the stylesheet. Even if they have an object name of their own.

          3. This is true even if the class isn't really the same. I tried making a subclass for the main frame ("AFrame"), that inherits from QFrame, but left the subframe as QFrame proper. The QFrame and QLabel still took on the AFrame's stylesheet.

          4. This isn't true in the other direction, though. The QFrame subwidget accepts the parent's AFrame stylesheet, but a stylesheet set directly with AFrame doesn't work (as expected).

          5. I can't find a way to delete the stylesheet of the subwidgets. I can set a different color, but I can't put them back to the default color.

            
            AFrame *vMainFrame = new AFrame(this);
            vMainFrame->setObjectName("AFrame");
            QHBoxLayout *vLayoutMainFrame = new QHBoxLayout(vMainFrame);
            ui->verticalLayout_2->addWidget(vMainFrame);
            
            QFrame *vSubFrame = new QFrame(this);
            vSubFrame->setObjectName("SOMEOTHERFRAME");
            vLayoutMainFrame->addWidget(vSubFrame);
            
            QHBoxLayout *vLayoutSubFrame = new QHBoxLayout(vSubFrame);
            QLabel* vLabel = new QLabel("womimwolmiplomw",this);
            vLabel->setObjectName("somelabel");
            vLayoutSubFrame->addWidget(vLabel);
            
            
            //main no color, sub frame no color, label is green
            //vMainFrame->setStyleSheet(QString("QFrame#somelabel {background-color: green}"));
            
            //main no color, sub frame + label green
            vMainFrame->setStyleSheet(QString("QFrame#SOMEOTHERFRAME {background-color: green}"));
            
            //no color
            //vMainFrame->setStyleSheet(QString("AFrame#SOMEOTHERFRAME {background-color: green}"));
            
            //main is blue, sub frame + label green
            //vMainFrame->setStyleSheet(QString("QFrame#AFrame {background-color: blue} QFrame#SOMEOTHERFRAME {background-color: green}"));
            
            //main is blue, sub frame + label blue
            //vMainFrame->setStyleSheet(QString("AFrame#AFrame {background-color: blue} QFrame#SOMEOTHERFRAME {}"));
            //vMainFrame->setStyleSheet(QString("QFrame#AFrame {background-color: blue} QFrame#SOMEOTHERFRAME {background-color: }"));
            //vMainFrame->setStyleSheet(QString("QFrame#AFrame {background-color: blue} QFrame#SOMEOTHERFRAME {}"));
            
            //no color
            //vMainFrame->setStyleSheet(QString("AFrame#AFrameNONEXISTING {background-color: blue} QFrame#SOMEOTHERFRAME {}"));
            //vMainFrame->setStyleSheet(QString("QFrame#AFrameNONEXISTING {background-color: blue} QFrame {}"));
            
            //no color for main, sub frame + label green
            //vMainFrame->setStyleSheet(QString("QFrame#AFrameNONEXISTING {background-color: blue} QFrame#SOMEOTHERFRAME {background-color: green}"));
            
            
              
          
          Best wishes,
          Angela
          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @angela2016 said in Set stylesheet to parent frame only:

          I'm trying to set a background color to a frame. That frame has a sub frame with a label in it. These two should have the default stylesheet/color

          Sorry I'm a bit late to the party. You want the top level frame to have a background color but its child frame plus its content like button/label not to inherit that, right? Then background-color: palette(base) or background-color: palette(window) on the child widget to cancel what is inherited from the parent widget seems to do what you want? And the latter is presumably the equivalent of what you have written in your code:

          #include <QFrame>
          #include <QPushButton>
          
          #include <QApplication>
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              QFrame f1;
              f1.setGeometry(50, 50, 200, 200);
              f1.setStyleSheet("background-color: red;");
              QFrame f2(&f1);
              f2.setGeometry(25, 25, 100, 100);
              f2.setStyleSheet("background-color: palette(window)");  // or palette(base)
              QPushButton pb(&f2);
              pb.setText("Hello");
              pb.setGeometry(20, 20, 50, 50);
              f1.show();
              return a.exec();
          }
          

          Screenshot 2026-02-14 095121.png

          Does this give you what you ask for?

          A 1 Reply Last reply
          0
          • JonBJ JonB

            @angela2016 said in Set stylesheet to parent frame only:

            I'm trying to set a background color to a frame. That frame has a sub frame with a label in it. These two should have the default stylesheet/color

            Sorry I'm a bit late to the party. You want the top level frame to have a background color but its child frame plus its content like button/label not to inherit that, right? Then background-color: palette(base) or background-color: palette(window) on the child widget to cancel what is inherited from the parent widget seems to do what you want? And the latter is presumably the equivalent of what you have written in your code:

            #include <QFrame>
            #include <QPushButton>
            
            #include <QApplication>
            
            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
                QFrame f1;
                f1.setGeometry(50, 50, 200, 200);
                f1.setStyleSheet("background-color: red;");
                QFrame f2(&f1);
                f2.setGeometry(25, 25, 100, 100);
                f2.setStyleSheet("background-color: palette(window)");  // or palette(base)
                QPushButton pb(&f2);
                pb.setText("Hello");
                pb.setGeometry(20, 20, 50, 50);
                f1.show();
                return a.exec();
            }
            

            Screenshot 2026-02-14 095121.png

            Does this give you what you ask for?

            A Offline
            A Offline
            angela2016
            wrote on last edited by
            #5

            @JonB Thanks, that's almost it. Line edits and check boxes aren't white enough, though, compared to the palette solution. It's all "label grey".

            JonBJ 1 Reply Last reply
            1
            • A angela2016

              @JonB Thanks, that's almost it. Line edits and check boxes aren't white enough, though, compared to the palette solution. It's all "label grey".

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #6

              @angela2016
              Oh, so after the palette(window) you are not back where you started, something has still changed? And same for palette(base)?

              1 Reply Last reply
              0
              • A Offline
                A Offline
                angela2016
                wrote on last edited by
                #7

                Not sure if you can see it well but here's a screenshot.

                The top example is my QPalette code. Second is the default without any setting of the background. The third and fourth window are with your code: palette(window) and palette(base) produce the same result. The label is as expected, but the line edit has the same color as the label, not as the default line edit.

                a50db0b1-7577-45cf-833f-06b884499f15-image.png

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  angela2016
                  wrote on last edited by
                  #8

                  Another observation: if I set the palette(window) background directly on the line edit, it becomes white, but the frame and label are blue (as expected). But if I set the palette(window) to both the subframe and the line edit, the line edit is still grey. It's all a bit odd.

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Hi,

                    If you want to change the background of only two widget, going the palette way would be easier:

                    QPalette *palette = vMainFrame->palette();
                    palette.setColor(QPalette.Window, QColor("green"));
                    vMainFrame->setPalette(palette);
                    

                    And the same for your other widget.

                    S Offline
                    S Offline
                    SimonSchroeder
                    wrote on last edited by
                    #9

                    @SGaist said in Set stylesheet to parent frame only:

                    If you want to change the background of only two widget, going the palette way would be easier

                    Changing the palette works unreliably on some OSes. I believe it only works properly with the Fusion style. But, if you want to go with a native look on Windows (and maybe macOS as well) you need to use stylesheets (which also have their problems). For portable code I can only advise againt using the palette.

                    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 Extensions
                    • Unsolved