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. setStyleSheet on QWidget does not have effect
Forum Updated to NodeBB v4.3 + New Features

setStyleSheet on QWidget does not have effect

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 3.9k 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.
  • S Offline
    S Offline
    skebanga
    wrote on last edited by skebanga
    #1

    I have an OuterWidget which has a QGridLayout and a single widget, InnerWidget, within:

    class OuterWidget : public QWidget
    {
        Q_OBJECT
    public:
        OuterWidget()
        {
    	    InnerWidget* innerWidget = new InnerWidget();
    	    QGridLayout* layout = new QGridLayout;
    	    layout->addWidget(innerWidget, 0, 0, 0, 4, Qt::AlignTop);
    	    setLayout(layout);
        }
    };
    

    InnerWidget does nothing other than set its background-color to black:

    class InnerWidget : public QWidget
    {
        Q_OBJECT
    public:
        InnerWidget(QWidget* parent) : QWidget(parent)
        {
    	    setStyleSheet("background-color: black");
        }
    };
    

    My expectation is for the widget to be displayed with a black background, but it doesn't:

    alt text

    If, however, I set the style sheet on the parentWidget, then it works as I expected:

    class InnerWidget : public QWidget
    {
        Q_OBJECT
    public:
        InnerWidget(QWidget* parent) : QWidget(parent)
        {
    	    parentWidget()->setStyleSheet("background-color: black");
        }
    };
    

    alt text

    According to the documentation:

    One consequence of this is that setting a style rule on a widget automatically gives it precedence over other rules specified in the ancestor widgets' style sheets or the QApplication style sheet

    This suggests that setting a style sheet on a widget itself should work, but this doesn't seem to be the case.

    Why does it work when I set the parent widget's style sheet, but not for the widget itself?

    JonBJ 1 Reply Last reply
    0
    • S skebanga

      I have an OuterWidget which has a QGridLayout and a single widget, InnerWidget, within:

      class OuterWidget : public QWidget
      {
          Q_OBJECT
      public:
          OuterWidget()
          {
      	    InnerWidget* innerWidget = new InnerWidget();
      	    QGridLayout* layout = new QGridLayout;
      	    layout->addWidget(innerWidget, 0, 0, 0, 4, Qt::AlignTop);
      	    setLayout(layout);
          }
      };
      

      InnerWidget does nothing other than set its background-color to black:

      class InnerWidget : public QWidget
      {
          Q_OBJECT
      public:
          InnerWidget(QWidget* parent) : QWidget(parent)
          {
      	    setStyleSheet("background-color: black");
          }
      };
      

      My expectation is for the widget to be displayed with a black background, but it doesn't:

      alt text

      If, however, I set the style sheet on the parentWidget, then it works as I expected:

      class InnerWidget : public QWidget
      {
          Q_OBJECT
      public:
          InnerWidget(QWidget* parent) : QWidget(parent)
          {
      	    parentWidget()->setStyleSheet("background-color: black");
          }
      };
      

      alt text

      According to the documentation:

      One consequence of this is that setting a style rule on a widget automatically gives it precedence over other rules specified in the ancestor widgets' style sheets or the QApplication style sheet

      This suggests that setting a style sheet on a widget itself should work, but this doesn't seem to be the case.

      Why does it work when I set the parent widget's style sheet, but not for the widget itself?

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

      @skebanga
      Is a possible answer to both you & @Smeeth in https://forum.qt.io/topic/91917/setting-the-background-color-of-widget-inside-another-widget:
      A plain QWidget with nothing on it does not show its background (if it's a parent it has something on it)?

      S 1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Add: setAutoFillBackground(true); in your InnerWidget constructor and you should be good to go.

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

        S 1 Reply Last reply
        0
        • JonBJ JonB

          @skebanga
          Is a possible answer to both you & @Smeeth in https://forum.qt.io/topic/91917/setting-the-background-color-of-widget-inside-another-widget:
          A plain QWidget with nothing on it does not show its background (if it's a parent it has something on it)?

          S Offline
          S Offline
          skebanga
          wrote on last edited by
          #4

          @JonB that is not correct

          1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

            Add: setAutoFillBackground(true); in your InnerWidget constructor and you should be good to go.

            S Offline
            S Offline
            skebanga
            wrote on last edited by
            #5

            @SGaist said in setStyleSheet on QWidget does not have effect:

            setAutoFillBackground

            From the documentation:

            Warning: Use this property with caution in conjunction with Qt Style Sheets. When a widget has a style sheet with a valid background or a border-image, this property is automatically disabled.

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

              I don't know whether a background color qualifies for that warning.

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

              1 Reply Last reply
              0
              • S Offline
                S Offline
                skebanga
                wrote on last edited by
                #7

                @sgaist Fair enough.

                Do you have any idea why setting the stylesheet on the parent works?

                Would you say that setting a stylesheet on the parent and having it cascade correctly to the child, whilst setting it on the widget itself does not work is a bug?

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

                  Hi
                  Cusom Widgets and stylesheets can be a bit funky together.
                  I suspected it would turn off background drawing for other reason than using images properties but did not verify it/test it.

                  However, always painting with the style seems to fix it.
                  Its mentioned somewhere in Docs, but couldn't find it again :)

                  class InnerWidget : public QWidget {
                    Q_OBJECT
                   public:
                    InnerWidget(QWidget* parent) : QWidget(parent) {
                      setStyleSheet("background-color: black");        
                    }
                   protected:
                    virtual void paintEvent(QPaintEvent* ) override {
                      QStyleOption opt;
                      opt.init(this);
                      QPainter p(this);
                      style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
                    }
                  };
                  
                  

                  It should then be black.

                  S 1 Reply Last reply
                  3
                  • mrjjM mrjj

                    Hi
                    Cusom Widgets and stylesheets can be a bit funky together.
                    I suspected it would turn off background drawing for other reason than using images properties but did not verify it/test it.

                    However, always painting with the style seems to fix it.
                    Its mentioned somewhere in Docs, but couldn't find it again :)

                    class InnerWidget : public QWidget {
                      Q_OBJECT
                     public:
                      InnerWidget(QWidget* parent) : QWidget(parent) {
                        setStyleSheet("background-color: black");        
                      }
                     protected:
                      virtual void paintEvent(QPaintEvent* ) override {
                        QStyleOption opt;
                        opt.init(this);
                        QPainter p(this);
                        style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
                      }
                    };
                    
                    

                    It should then be black.

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

                    Hello everyone, thank you for the answers.

                    @mrjj You are correct, I also just found this page that describes exactly what you are saying about overridding the paintEvent class.

                    Thank you everyone!

                    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