multiple Q_ENUM_NS in one namespace OK?
Solved
General and Desktop
-
Hi all -
This is what I'm trying to do:
// equipmentitem.h namespace EquipmentNS { Q_NAMESPACE enum Category { ... }; Q_ENUM_NS(Category) enum PowerState { POWER_OFF, POWER_ON }; Q_ENUM_NS(PowerState) } ... // main.cpp // register the Equipment state enums so they can be used in QML. qmlRegisterUncreatableMetaObject( EquipmentNS::staticMetaObject, // meta object created by Q_NAMESPACE macro "equipmentNS", // import statement (can be any string) 1, 0, // major and minor version of the import "EquipmentNS", // name used in QML (does not have to match C++ name) "Error: only enums" // error in case someone tries to create an object );
A few questions:
is this legal?dumb question; disregard.- is this correct?
- is this complete?
- is this a bad idea?
- how do I reference the respective enums in QML? I'd have expected it to be something like this:
EquipmentNS.POWER_ON
But that gives me an undefined error.
Thanks...
-
The answer is "yes," it works. I had some mistakes in my attempt which caused me to question it, but it works fine when you do it right.
In simplest terms (credit to this post):
// header file of your choice namespace EquipmentNS { Q_NAMESPACE enum Category { ... }; Q_ENUM_NS(Category) enum PowerState { POWER_OFF, POWER_ON }; Q_ENUM_NS(PowerState) } // main.cpp qmlRegisterUncreatableMetaObject( EquipmentNS::staticMetaObject, // meta object created by Q_NAMESPACE macro "equipment.enums", // used in qml import statement 1, 0, // major and minor version of the import "EquipmentNS", // name used in QML (does not have to match C++ name) "Error: only enums" // error in case someone tries to create an object ); // your qml file import equipment.enums // same as 2nd argument in call to qmlRegisterUncreatableMetaObject(). ... console.log(EquipmentNS.POWER_ON)
-
M mzimmers has marked this topic as solved on
-
Yes. It works. However you won't be able to have same enum names since one will shadows another one.
namespace EquipmentNS { Q_NAMESPACE enum Category { POWER_OFF = 100, ... }; Q_ENUM_NS(Category) enum PowerState { POWER_OFF, #Here value 0 will shadow value 100 POWER_ON }; Q_ENUM_NS(PowerState) }