Scoped enums, Meta Object and Stylesheets
-
Dear all,
How can I register a scoped enum to the meta-object system ?
I'd like be able to change the value of a scoped enum property via stylesheet but i'm not able to make my code work:class MyClass : public QWidget { Q_OBJECT; Q_ENUMS(Priority); Q_PROPERTY(Priority ThePriority MEMBER priority); public: enum struct Priority { High, Low, VeryHigh, VeryLow }; MyClass(QObject *parent = 0) {} ~MyClass() {} void setPriority(Priority ipriority) { priority = ipriority; } Priority getPriority() const { return priority; } private: Priority priority { Priority::High }; };
int main(int argc, char *argv[]) { QApplication a(argc, argv); a.setStyleSheet(QStringLiteral("MyClass { qproperty-ThePriority: VeryLow;}")); MyClass myClass; myClass.ensurePolished(); Q_ASSERT(myClass.getPriority() == MyClass::Priority::VeryLow); // ASSERT! getPriority() == MyClass::Priority::High return a.exec(); }
Any idea ?
Regards
Edit: Fix errors in source code sample -
Hi,
AFAIK, you can't set a property through style sheet. You can create a style sheet that reacts on a property change though.[edit: memory was wrong SGaist]
-
Yes you can: Setting QObject Properties
-
Which version of Qt are you using ?
-
From your link you would need to use:
qproperty-priority
instead.
If this doesn't work (and it's just a wild guess) but could you register that enum as an alias in the metatype system, e.g.qRegisterMetatype<Priority>("Priority")
in your class' constructor. -
@SGaist
I'm using Qt 5.9.4 x64@kshegunov
You are right, there was typo in my sample. I've edited the code from the main post.
I had no success withqRegisterMetatype
, the code doesn't compile.
As you suggested, I've tried this:MyClass(QObject *parent = 0) { qRegisterMetatype<Priority>("Priority") }
-
Please use
enum class
.moc currently doesn't support
enum struct
, there's a patch in the work though for that.