Q_ENUM(enum_name) works with only enums declared inside class. srsly?
-
Let's look at example from documentation which is copied next.
class Message : public QObject { Q_OBJECT Q_ENUMS(Status) Q_PROPERTY(Status status READ status NOTIFY statusChanged) public: enum Status { Ready, Loading, Error }; Status status() const; signals: void statusChanged(); };
In this example we declare our enumeration declared inside class with Q_ENUM(). It is ok. But what if we want to use Globally declared enumeration. Qt gives errors. How can we achieve operating with enumerations declared outside the class?
-
Hi,
You can't, Q_ENUMS need a meta object provided either by the QObject class or the Q_GADGET macro
-
This is an old topic, but still comes up as one of the first Google results. I just wanted to add:
If the main goal is to use the enum as a property, it is sufficient to just use Q_DECLARE_METATYPE after the globally-declared enumeration.
If you need the other conveniences of Q_ENUM, then SGaist's reply applies.
-
@Kofr said in Q_ENUM(enum_name) works with only enums declared inside class. srsly?:
How can we achieve operating with enumerations declared outside the class?
This is possible since Qt 5.8 where the
Q_NAMESPACE
macro was introduced. This enables the generation of a MOC object also for namespaces.namespace MyNamespace { Q_NAMESPACE enum MyEnum { Foo, Bar }; Q_ENUM_NS(MyEnum) }
Note the
Q_ENUM_NS
macro here. There is also aQ_FLAG_NS
macro available. -
Let me know if this question does not belong on this post. I am on Windows 10 and my project configuration is set to use Qt 5.11.0 MSVC2015 64 bit. I created a new Qt Quick application and copy pasted the namespace deceleration as posted by raven-worx and compiled. I get the following errors which disappear if I remove the Q_NAMESPACE and Q_ENUM_NS statements.
qtprojects\QEnum\myenum.h:7: error: C2143: syntax error: missing ';' before 'enum [tag]'
qtprojects\QEnum\myenum.h:8: error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int
qtprojects\QEnum\myenum.h:13: error: C2143: syntax error: missing ';' before '}'
qtprojects\QEnum\myenum.h:13: error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int
QTProjects\build-QEnum-Desktop_Qt_5_11_0_MSVC2015_64bit-Debug\debug\moc_myenum.cpp:65: error: C2039: 'staticMetaObject': is not a member of 'Enums'
qtprojects\QEnum\myenum.h:4: see declaration of 'Enums'I am also new so I feel like I may be missing something simple. The only things that exist are the default main.cpp and main.qml and a header file with the above pasted code. Thanks in advance.
-
You need to include QObject to make it work
-
To add to SGaist' response, below is an example of Q_ENUM in a
struct
withQ_GADGET
:struct Message { Q_GADGET public: enum Status { Ready, Loading, Error }; Q_ENUM(Status) };
This precludes using signals and slots within the
struct
, is a lightweight solution for properties or unit test arguments. This works as far back as Qt 5.5.