Q_ENUMS problem
-
wrote on 12 Jun 2011, 14:58 last edited by
How can i use an enum that was declared in another class in qml ?
-
wrote on 12 Jun 2011, 15:32 last edited by
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.
-
wrote on 12 Jun 2011, 15:35 last edited by
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]
-
wrote on 12 Jun 2011, 15:53 last edited by
Which class exactly do you want to use in qml?
-
wrote on 12 Jun 2011, 15:54 last edited by
I want to use class that i create myself (inherit QObject)
-
wrote on 12 Jun 2011, 16:31 last edited by
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). -
wrote on 13 Jun 2011, 06:15 last edited by
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? -
wrote on 13 Jun 2011, 06:50 last edited by
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()."
-
wrote on 13 Jun 2011, 07:04 last edited by
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" ?
-
wrote on 13 Jun 2011, 08:57 last edited by
To use enums in qml you need to
- use Q_ENUMS
- use Q_DECLARE_METATYPE(YourClass::YourEnum)
- use qmlRegisterUncreatableType<YourClass>("YourPackage", 1, 0, "YourClass", "")
-
wrote on 13 Jun 2011, 10:33 last edited by
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 ? -
wrote on 13 Jun 2011, 14:59 last edited by
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();
};
@ -
wrote on 5 Sept 2014, 08:22 last edited by
Hello, You can register enum to meta data system :
@qRegisterMetaTypeYourClass::YourEnum("YourClass::YourEnum");
@It runs ok
-
wrote on 11 Sept 2014, 15:58 last edited by
[[blank-post-content-placeholder]]