Nominate our 2022 Qt Champions!

[Solved]Create enum for qml

  • With the help of Q_PROPERTY, we could create property for qml
    what about enumerator?Is it possible to export the enumerator of c++
    to qml side?

  • Moderators

    There is no built-in way to have enums in QML. And it's not likely to be introduced soon - there are some technical hurdles on the way.

  • Thanks, I will try another solution

  • You can use Q_ENUMS to export enum names to the QML typesystem. Internally, all enums are just treated as ints so if something doesn't work, change your property type to int and things should start working ;-)

    class MyType : public QObject
    Q_PROPERTY(MyEnum enumProp READ enumPropAccessor CONSTANT)

    enum MyEnum {
    FirstValue = 1,
    SecondValue = 2

    MyType(QObject *parent = 0) : QObject(parent) {}
    ~MyType() {}
    MyEnum enumPropAccessor() const { return FirstValue; }


    In QML, you should be able to do:

    MyType {
    property int someValue: MyType.SecondValue


Log in to reply