[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_OBJECT
    Q_PROPERTY(MyEnum enumProp READ enumPropAccessor CONSTANT)
    Q_ENUMS(MyEnum)

    public:
    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
    }
    @

    Cheers,
    Chris.


Log in to reply
 

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