Define Enum in coed header and Use it in qml
-
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 } }
-
@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
}
}