How to convert enum to QString?

后端 未结 6 1120
庸人自扰
庸人自扰 2020-12-08 06:38

I am trying to use the Qt reflection for converting enum to QString.

Here is the part of code:

class ModelApple
{
    Q_GADGET
    Q_ENUMS(AppleType)         


        
6条回答
  •  既然无缘
    2020-12-08 07:10

    The following should get you going:

    QString convertEnumToQString(ModelApple::AppleType type) {
        const QMetaObject metaObject = ModelApple::staticMetaObject;
        int enumIndex = metaObject.indexOfEnumerator("AppleType");
        if(enumIndex == -1) {
            /* The enum does not contain the specified enum */
            return "";
        }
        QMetaEnum en = metaObject.enumerator(enumIndex);
        return QString(en.valueToKey(type));
    }
    

提交回复
热议问题