Q_ENUMS problem
-
The problem is that if i define an enum in one class and than i want to use in in another class:
@
ClassA
{
...
enum someEnum{E1, E2, E3}
...
}ClassB
{
...
Q_ENUMS(ClassA::someEnum)
...
}
@and than to use in in qml it does not work
[EDIT: code formatting, please use @-tags, Volker]
-
OK, after doing some research and testing, here's the situation:
The only way you can make it work is using the Macros Q_GADGET (or of course inherit QObject etc.) and Q_ENUMS(someEnum) within ClassA (I didn't find any other way, but maybe somebody else on the forum has a solution). -
Like @loladiro pointed out.. unless you intend to use the enum in signal/slot connections or something which needs Qt's metaobject system to recognize your enum, you just access it as A::someEnum in B.
From Qt Documentation : "If you want to register an enum that is declared in another class, the enum must be fully qualified with the name of the class defining it. In addition, the class defining the enum has to inherit QObject as well as declare the enum using Q_ENUMS()."
-
To use enums in qml you need to
- use Q_ENUMS
- use Q_DECLARE_METATYPE(YourClass::YourEnum)
- use qmlRegisterUncreatableType<YourClass>("YourPackage", 1, 0, "YourClass", "")
-
Ok I took the time to make an example (excuse the sloppy naming). This works and is accessible in QML:
@class Test2
{
Q_GADGET
public:
Q_ENUMS(Test)
Test2() {}
enum Test { V1=100, V2, V3 };
};class EnumTest : public QObject
{
Q_OBJECT
public:
Q_PROPERTY(Test2::Test test23 READ get3 WRITE set3 NOTIFY changed3 )
EnumTest() : test23(Test2::V3) {}
Test2::Test test23;
Test2::Test get3() {return test23;}
void set3(Test2::Test t) {test23=t;emit changed3();}
signals:
void changed3();
};
@
This doesn't (and I found no way to make it work without changing the Test2 class):
@
class Test2
{
public:
Test2() {}
enum Test { V1=100, V2, V3 };
};class EnumTest : public QObject
{
Q_OBJECT
public:
Q_PROPERTY(Test2::Test test23 READ get3 WRITE set3 NOTIFY changed3 )
Q_ENUMS(Test2::Test)
EnumTest() : test23(Test2::V3) {}
Test2::Test test23;
Test2::Test get3() {return test23;}
void set3(Test2::Test t) {test23=t;emit changed3();}
signals:
void changed3();
};
@ -
Hello, You can register enum to meta data system :
@qRegisterMetaTypeYourClass::YourEnum("YourClass::YourEnum");
@It runs ok