When add Q_OBJECT , the styleSheet is useless.
-
Hey, I have a question.
I have a class,class SettingsItem : public QWidget { public: explicit SettingsItem(QWidget* parent = nullptr) : QWidget(parent) { setProperty("widgetType", "settingsItem"); } ~SettingsItem() { } };I add it to a qwidget
SettingsItem* m_item = new SettingsItem(); ui->widget_4->layout()->addWidget(m_item);I set the QAppliaction style sheet like this
QApplication a(argc, argv); QFile qssFile(":/MainWindow/qss"); QByteArray baQss; if (qssFile.open(QIODevice::ReadOnly)) { baQss = qssFile.readAll(); } a.setStyleSheet(baQss); MainWindow w; w.show(); return a.exec();the qss file:
QWidget[widgetType=settingsItem] { border: 2px solid red; border-radius: 2px; }when running, the settingsItem's border is change

but when i add "Q_OBJECT" to the SettingsItem
class SettingsItem : public QWidget { Q_OBJECT public: explicit SettingsItem(QWidget* parent = nullptr) : QWidget(parent) { setProperty("widgetType", "settingsItem"); } ~SettingsItem() { } };This border is gone.Why?

And why don't i add Q_OBJECT, the styleSheet is in effect. -
It's a little known quirk of the stylesheets. When you subclass QWidget and your class has a meta object you need to implement
paintEventlike this:void SettingsItem::paintEvent(QPaintEvent*) { QStyleOption opt; opt.initFrom(this); QPainter p(this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); }It's documented here at the end of the table, but it's easy to miss. You don't have to do that when you're subclassing any other widget like line edit or button, just with plain QWidget.
-
It's a little known quirk of the stylesheets. When you subclass QWidget and your class has a meta object you need to implement
paintEventlike this:void SettingsItem::paintEvent(QPaintEvent*) { QStyleOption opt; opt.initFrom(this); QPainter p(this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); }It's documented here at the end of the table, but it's easy to miss. You don't have to do that when you're subclassing any other widget like line edit or button, just with plain QWidget.
@Chris-Kawa said in When add Q_OBJECT , the styleSheet is useless.:
You don't have to do that when you're subclassing any other widget like line edit or button, just with plain QWidget.
Thank u. But i don't understand this "You don't have to do that when you're subclassing any other widget like line edit or button, just with plain QWidget." .
-
@Chris-Kawa said in When add Q_OBJECT , the styleSheet is useless.:
You don't have to do that when you're subclassing any other widget like line edit or button, just with plain QWidget.
Thank u. But i don't understand this "You don't have to do that when you're subclassing any other widget like line edit or button, just with plain QWidget." .
@Gaobo I mean if you have this
class Something : public QWidgetyou need that custom paintEvent, but if you have for example this
class Something : public QLineEdit //or any other widget typethen you don't.
-
@Gaobo I mean if you have this
class Something : public QWidgetyou need that custom paintEvent, but if you have for example this
class Something : public QLineEdit //or any other widget typethen you don't.
@Chris-Kawa
Oh, thank u. i understand. It's mean, if havn't a meta object, i don't need to custom paintEvent, right? -
@Gaobo I mean if you have this
class Something : public QWidgetyou need that custom paintEvent, but if you have for example this
class Something : public QLineEdit //or any other widget typethen you don't.
@Chris-Kawa
Oh, I think the reason for that the styleSheet is in effect when i don't add Q_OBJECT is:- when i not add Q_OBJECT, the "settingsItem" don't have parent, because it don't have Q_OBJECT, so ,the "settingsItem" background is itself background.
- when i add Q_OBJECT, and not "setAttribute(Qt::WA_StyledBackground)", it's background is the background of its parent object.
- the "WA_StyledBackground" is specify that it's background determined by style sheet.