How to convert enum to QString?

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

    For the global Enum declaring use this in any header file:

    namespace YourNamespace {
    
    Q_NAMESPACE
    
    enum YourEnum: int {
    
        EnumValue1,
        EnumValue2
    };
    Q_ENUM_NS(YourEnum)
    
    }
    

    and this where you want to get Enum description:

    QMetaEnum metaEnum = QMetaEnum::fromType();
    qDebug() << "Enum description: " << metaEnum.name() << "::" << metaEnum.valueToKey(YourEnum::EnumValue2);
    

提交回复
热议问题