简体   繁体   中英

enum type as property type in C++ class for QML

I have the similar situation as in this answer . However I use the property in QML widgets. I am successful to register my class with enum (using qmlRegisterUncreatableType ) and I can do the following:

console.log(ClassB.A) // return me an integer

Unfortunately when I take the property in QML

console.log(myAObjfromCpp.test) // I get QVariant(ClassB::TestEnum)

And those two return value are not comparable with each other. How properly create a property from cpp of enum type defined in a another class and use it in QML?

It is explained in the docs here .

As per it you need to use declare it using Q_ENUMS . For eg.:

//C++ code
Q_ENUMS(Status)
Q_PROPERTY(Status status READ status NOTIFY statusChanged)

enum Status {
    Ready,
    Loading,
    Error
};

//QML code
console.log(Message.Ready) //Message being the class in which enum is defined

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM