Why Inherit QWidget cant set background color If use Q_OBJECT In Qt6.5
-
If i use Q_OBJECT in InheritWidget, i can see
if i dont use it, i can see
Here is my code, I dont understand why my inherit widget cant set background color.
#include <QWidget> #include "Mywidget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication app(argc, argv); MyWidget *mywidget = new MyWidget(nullptr); mywidget->SetSubWidget(); mywidget->show(); return app.exec();; }
#pragma once #include <QWidget> class InheritWidget : public QWidget { Q_OBJECT public: InheritWidget(QWidget* parent); };
#include "InheritWidget.h" InheritWidget::InheritWidget(QWidget* parent) :QWidget(parent) { }
#pragma once #include <QWidget> class MyWidget: public QWidget { public: MyWidget(QWidget *parent); void SetSubWidget(); };
#include "Mywidget.h" #include "InheritWidget.h" MyWidget::MyWidget(QWidget* parent) : QWidget(parent) { this->setGeometry(0,0, 300, 300); this->setWindowFlags(Qt::FramelessWindowHint); this->setStyleSheet("background-color:black;"); this->setAutoFillBackground(true); } void MyWidget::SetSubWidget() { InheritWidget*subWidget = new InheritWidget(this); subWidget->setGeometry(100, 100, 100, 100); subWidget->setStyleSheet("background-color:yellow;"); subWidget->setAutoFillBackground(true); subWidget->show(); QWidget*ssubWidget = new QWidget(this); ssubWidget->setGeometry(0, 0, 100, 100); ssubWidget->setStyleSheet("background-color:green;"); ssubWidget->setAutoFillBackground(true); ssubWidget->show(); }
-
Because style sheets are propagated to the children (if the children a correct QMetaObjects).
-
@Christian-Ehrlicher Is there a way to set the background color of a child widget?
-
@Quiccz said in Why Inherit QWidget cant set background color If use Q_OBJECT In Qt6.5:
Is there a way to set the background color of a child widget?
If you use css on the parent then you have to set the background color of the child also with css.
-
@Quiccz
Not sure exactly why, and not sure if you are saying this behaviour is to do with specific 6.5 version.One thing: you are inheriting from
QWidget
and you use stylesheet with setAutoFillBackgroundWarning: 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.
The other thing comes from How to Change the Background Color of QWidget
Note: If you subclass a custom widget from QWidget, then in order to use the StyleSheets you need to provide a paintEvent to the custom widget :
Only @Christian-Ehrlicher knows how/whether this applies here....
-
@Christian-Ehrlicher Thanks, i use QPalette works
-
@JoeCFD
I know that. The issue is from the Qt wiki, https://wiki.qt.io/How_to_Change_the_Background_Color_of_QWidget, Using Style Sheet sub-heading:Note: If you subclass a custom widget from QWidget, then in order to use the StyleSheets you need to provide a paintEvent to the custom widget :
void CustomWidget::paintEvent(QPaintEvent* event) { QStyleOption opt; opt.init(this); QPainter p(this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); QWidget::paintEvent(event); }
And (at least I thought) I recall having to do this, recently. I don't know how come the OP does not need same. There you are.
-
-