How to convert enum to QString?

后端 未结 6 1113
庸人自扰
庸人自扰 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:19

    How about:

    QString convertEnumToQString(ModelApple::AppleType type)
    {
        const QMetaObject &mo = ModelApple::staticMetaObject;
        int index = mo.indexOfEnumerator("AppleType");
        QMetaEnum metaEnum = mo.enumerator(index);
        return metaEnum.valueToKey(type);
    }
    

    UPDATED: For Qt 5.5, see this answer

提交回复
热议问题