Q_ENUMS problem
-
How can i use an enum that was declared in another class in qml ?
-
You don't have to call Q_ENUMS in the class the enum was defined in. If you use the full qualifier name, you can do it in basically any class. Hope that helps.
-
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]
-
Which class exactly do you want to use in qml?
-
I want to use class that i create myself (inherit QObject)
-
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). -
I do use Q_ENUMS in class A but when i try to use that enum in a Q_INVOKABLE function of class B (in QMl) i can't.
Do you know why? -
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()."
-
The problem is that i want to use it in QML - not in c++.
what does it mean "must be fully qualified with the name of the class defining it" ?
-
To use enums in qml you need to
- use Q_ENUMS
- use Q_DECLARE_METATYPE(YourClass::YourEnum)
- use qmlRegisterUncreatableType<YourClass>("YourPackage", 1, 0, "YourClass", "")
-
I know how to use enum with Q_ENUMS.
My question is:
How can i use enum that was declared in another class in qml ? -
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
-
[[blank-post-content-placeholder]]