setStyleSheet on QWidget does not have effect
-
I have an
OuterWidget
which has aQGridLayout
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:
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"); } };
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?
-
I have an
OuterWidget
which has aQGridLayout
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:
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"); } };
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 plainQWidget
with nothing on it does not show its background (if it's a parent it has something on it)? -
Hi,
Add:
setAutoFillBackground(true);
in your InnerWidget constructor and you should be good to go. -
@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 plainQWidget
with nothing on it does not show its background (if it's a parent it has something on it)? -
Hi,
Add:
setAutoFillBackground(true);
in your InnerWidget constructor and you should be good to go.@SGaist said in setStyleSheet on QWidget does not have effect:
setAutoFillBackground
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.
-
I don't know whether a background color qualifies for that warning.
-
-
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.
-
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.