What I would like to do is use the below enum in c++:
class MyClass : public QQuickItem {
Q_OBJECT
Q_PROPERTY(MyEnum enumValue READ getEnumValue)
public:
enum MyEnum{
MyEnumElement = 0
};
Q_ENUM(MyEnum)
slots:
MyEnum getEnumValue(){
return enumValue;
}
private:
MyEnum enumValue = MyEnumElement;
};
In such a way in QML:
MyClass {
id: myClass
}
Text {
text: "enumValue is: " + myClass.enumValue
}
That would result in enumValue is MyEnumElement
instead of enumValue is 0
which is the case now. The solution I have now is the following, however inelegant:
class MyClass : public QQuickItem {
Q_OBJECT
Q_PROPERTY(MyEnum enumValue READ getEnumValue)
public:
enum MyEnum{
MyEnumElement = 0
};
Q_ENUM(MyEnum)
QMetaEnum MyEnum_MetaEnum = QMetaEnum::fromType<MyEnum>();
Q_INVOKABLE QString getEnumString(MyEnum enumVal){
return MyEnum_MetaEnum.valueToKey(enumVal);
}
slots:
MyEnum getEnumValue(){
return enumValue;
}
private:
MyEnum enumValue = MyEnumElement;
};
And:
MyClass {
id: myClass
}
Text {
text: "enumValue is: " + myClass.getEnumString(myClass.enumValue)
}
Is there a better way to do this?