Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using c++ enum in QML as string

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?

like image 660
Ayberk Özgür Avatar asked Jun 06 '16 17:06

Ayberk Özgür


1 Answers

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)
    ...
like image 78
Ayberk Özgür Avatar answered Nov 14 '22 02:11

Ayberk Özgür