Q_ENUMS, Q_DECLARE_METATYPE and QML
-
I have a class which (much simplified) is:
@class MyClass : public QObject
{
Q_OBJECTQ_ENUMS(MyEnum)
Q_PROPERTY(MyEnum dataValue READ dataValue NOTIFY dataValueChanged)public:
enum MyEnum {
eOne,
eTwo
}...
};
Q_DECLARE_METATYPE(MyClass::MyEnum)
@The enum is declared in the object with Q_ENUMS so that it's visible within my QML, and the declare metatype is used so it can be accessed from a QVariant using the .value<T> call.
The problem is that when I use the declare metatype the enum is no longer visible from the QML... My guess is that the enum is already in the meta object system by it being in the class, but this means I can't use it in the QVariant::value call.
It feels like an obvious "You just can't do that!" but I can't find anything saying it isn't possible, any ideas?
Thanks,
Steve -
QML compares enum types with integers! If you register your Enum type, it's not an integer anymore.
To work around this, you could write the following:
@
class MyClass : public QObject
{
Q_OBJECTQ_ENUMS(MyEnum)
Q_PROPERTY(int dataValue READ declarativeDataValue NOTIFY dataValueChanged) <--- Changedpublic:
enum MyEnum {
eOne,
eTwo
}MyEnum dataValue() const;
int declarativeDataValue() const; <--- Added...
};
Q_DECLARE_METATYPE(MyClass::MyEnum)
@ -
This does fix the problem (and it's the workaround we're using).
I don't understand why the original code works perfectly without the Q_DECLARE_METATYPE, but when I use that it doesn't... QML comparing it to int shouldn't be affected by the declare metatype.