8

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?

0

1 Answer 1

7

I found the following to work:

class MyClass : public QQuickItem {
    Q_OBJECT
    Q_PROPERTY(QVariant enumValue READ getEnumValue)

public:
    enum MyEnum{
        MyEnumElement = 0
    };
    Q_ENUM(MyEnum)

slots:
    QVariant getEnumValue(){
        return QVariant::fromValue(enumValue);
    }

private: 
    MyEnum enumValue = MyEnumElement;
};

QML side:

MyClass {
    id: myClass
}

Text {
    text: "enumValue is: " + myClass.enumValue.toString()
}

Using without .toString() still causes the QVariant to be cast to an int. I'm still open to more elegant solutions.

Edit:

As expected, this breaks switch and === on the QML/Javascript side. switch(enumValue.valueOf()) and == must be used when comparing to enum ints exported by Q_ENUM(), such as MyClass.MyEnumElement:

switch(myClass.enumValue.valueOf()){
    case MyClass.MyEnumElement:
        ...
        break;
}

and

if(myClass.enumValue == MyClass.MyEnumElement)
    ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.