setStyleSheet on QWidget does not have effect



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



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


  • Lifetime Qt Champion

    Hi,

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



  • @JonB that is not correct



  • @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.


  • Lifetime Qt Champion

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



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


  • Qt Champions 2017

    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.



  • 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!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.