Unsolved C++ enum to QML ComboBox
-
Hello is there any way to use directly Enum from C++ as QML ComboBox model?
-
@Babs Hi,
If your enums are defined in a QObject subclass, you can extract list of them quite easily using Qt Meta-Object System
-
@Gojir4
Should the OP also possibly read through https://woboq.com/blog/q_enum.html? Does that (Q_ENUM
) give a convenient wrapper? -
@JonB You're right. I should have precised that Q_ENUM macro is required to register the enum in the Meta-Object system.
What do you mean by "a convenient wrapper" ? -
@Gojir4
I don't do Qt C++. I didn't know whether you can make meta-object calls directly without the need for that macro. Or of course you could write the code directly, for all I know you might have said whatever theQ_ENUM
macro is isn't a "convenient wrapper to access the meta-system" here, you don't like it or you don't want to have to write C++ to access from QML, or something like those. -
@Gojir4 I define my enum class like this
class QTOBJECTDICTONNARY_EXPORT CanOpenNmtCmdSpecifs { Q_GADGET public: enum Enum { StartNode = 0x01, StopNode = 0x02, SetPreOp = 0x80, ResetNode = 0x81, ResetComm = 0x82, }; Q_ENUM (Enum) }; typedef CanOpenNmtCmdSpecifs::Enum CanOpenNmtCmdSpecif;
And try to expose it to QML in that way
qmlRegisterType<CanOpenNmtCmdSpecif>("NMTCmd",1,0,"NMTCmd");
The problem is that i get the error : staticMetaObject' is not a member of 'CanOpenNmtCmdSpecifs::Enum'
const char *className = T::staticMetaObject.className();
~~~~~~~~~~~~~~~~~~~~^
Is the Q_GADGET use instead of Q_OBJECT the probblem? -
@Babs You're declaring the enum and not the type here:
qmlRegisterType<CanOpenNmtCmdSpecif>("NMTCmd",1,0,"NMTCmd");
Should not it be :
qmlRegisterType<CanOpenNmtCmdSpecifs>("NMTCmd",1,0,"NMTCmd");
Also I'm not sure it works with the typedef, and it's unecessary in c++ you can write.
enum CanOpenNmtCmdSpecif{ StartNode = 0x01, StopNode = 0x02, SetPreOp = 0x80, ResetNode = 0x81, ResetComm = 0x82, }; Q_ENUM (CanOpenNmtCmdSpecif)
-
@Gojir4 Indeed the register doesn't work with typeDef. Thank you for your assistance.
Regards, -
@Babs You're welcome ! Please don't forget to set the post as solved if your issue is fixed. Thanks !