How to use common enum in both C++ and qml.??

  • I have an enum in enumFile.h.
    I want to use it in signal-slot functions in a class, so i used qRegisterMetaType<enum>("enum") in constructor.
    I also want to use it in qml, for which i have to have a copy of that enum in class which i bind with qml and use Q_ENUM(enum) macro to make it visible in qml.

    so is there any method by which i can have a common enum in enum.h, and use it in signal-slot and also in qml files.?

  • Hey, did you find a solution?
    Thank you,

  • I would also be interested in this solution.

  • hey CreMindES. Any expertise in the Q_DECLARE_METATYPE maybe to solve this issue, in conjonction with the qmlRegisterUncreatableType?

    I even tried to define a macro in an external file to declare my enum, and then simply put my macro like this:
    @MyClass: public QObject


    and MACRO being defined in an external file.h like:
    #define MACRO
    enum MyEnum { A, B, C};

    But even this does not work, I always get an undefined value in the QML when trying to use A, B or C

    Any more luck?

  • So OK the moc does not preprocess my macro apparently, and therefore the Q_ENUMS does not know about MyEnum while the moc is preprocessing it. But what is the solution then?


Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.