Define Enum in coed header and Use it in qml
-
wrote on 16 May 2016, 12:13 last edited by p3c0
I would like to define a enum in cpp for globally to use in my qml.
I referenced the documentation and follow the usage to make my own.
However, it doesn't work in my case. Probably I used it wrong way.
Could you please help me to have right code?This is my code
type.hnamespace AName { namespace BName { enum YourType { NONE = 0, TypeA = 1, TypeB = 2 }; } }
A.h
typedef enum AName::BName::YourType MyType Class A { Q_OBJECT Q_ENUM(MyType) ... }
A.cpp
A::A() rootContext()->setContextProperty("qmlA", this);
xxx.qml
(I am able to access qmlA::xxxxx())Text { property var type: 0 Component.onCompleted: { ** if (type == qmlA.TypeA)** (!!!!!! This doesn't work it returns undefined !!!!!!) return } }
-
I would like to define a enum in cpp for globally to use in my qml.
I referenced the documentation and follow the usage to make my own.
However, it doesn't work in my case. Probably I used it wrong way.
Could you please help me to have right code?This is my code
type.hnamespace AName { namespace BName { enum YourType { NONE = 0, TypeA = 1, TypeB = 2 }; } }
A.h
typedef enum AName::BName::YourType MyType Class A { Q_OBJECT Q_ENUM(MyType) ... }
A.cpp
A::A() rootContext()->setContextProperty("qmlA", this);
xxx.qml
(I am able to access qmlA::xxxxx())Text { property var type: 0 Component.onCompleted: { ** if (type == qmlA.TypeA)** (!!!!!! This doesn't work it returns undefined !!!!!!) return } }
@Jehyeok AFAIK setting context property won't work. You will need to register that class using
qmlRegisterType
.import
it in QML and you are ready to use it. -
@Jehyeok AFAIK setting context property won't work. You will need to register that class using
qmlRegisterType
.import
it in QML and you are ready to use it.wrote on 16 May 2016, 17:20 last edited by@p3c0 Could you give me some details ?
I tried this but it doesn't work.
namespace AName
{
namespace BName
{
enum YourType
{
NONE = 0, TypeA = 1, TypeB = 2
};
}
}A.h
Class A { Q_OBJECT
public:
typedef enum AName::BName::YourType MyType
Q_ENUM(MyType)
...
}main.cpp
qmlRegisterTypenamespaces::A("Namespaces.A", 1, 0, "A");.qml
import Namespaces.A 1.0
Text {
property var type: 0
Component.onCompleted: {
** if (type == A.TypeA)** (!!!!!! This doesn't work it returns undefined !!!!!!)
return
}
} -
@p3c0 Could you give me some details ?
I tried this but it doesn't work.
namespace AName
{
namespace BName
{
enum YourType
{
NONE = 0, TypeA = 1, TypeB = 2
};
}
}A.h
Class A { Q_OBJECT
public:
typedef enum AName::BName::YourType MyType
Q_ENUM(MyType)
...
}main.cpp
qmlRegisterTypenamespaces::A("Namespaces.A", 1, 0, "A");.qml
import Namespaces.A 1.0
Text {
property var type: 0
Component.onCompleted: {
** if (type == A.TypeA)** (!!!!!! This doesn't work it returns undefined !!!!!!)
return
}
}@Jehyeok Since Q_Enum is declared in ClassA, register ClassA.
-
@p3c0 I am able to use a enum that I declared in the resisted class. But, typedef enum didn't work.
Do you know how to do it? Otherwise I have to have duplicated enum in two files...@Jehyeok Are those enums in the class with Q_OBJECT markers ? You can try creating a similar class and then include it in the registered class.
-
@Jehyeok Are those enums in the class with Q_OBJECT markers ? You can try creating a similar class and then include it in the registered class.
1/7