Qt:如何将枚举转换为QString [英] Qt: How to convert enum to QString

查看:195
本文介绍了Qt:如何将枚举转换为QString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是代码的一部分:

  class ModelApple 
{
Q_GADGET
Q_ENUMS(AppleType)
public:
枚举AppleType {
大,

}
}

这里是我尝试做:

  convertEnumToQString(ModelApple :: Big)

返回Big



这可能吗?
如果您对 convertEnumToQString 有任何想法,请分享

解决方案

您需要使用注册枚举类型的 Q_ENUM 宏与元对象系统。

 枚举AppleType {
Big,
Small
};
Q_ENUM(AppleType)

现在,您可以使用 QMetaEnum 类访问有关枚举器的元数据。

  QMetaEnum metaEnum = QMetaEnum :: fromType< ModelApple :: AppleType>(); 
qDebug()<< metaEnum.valueToKey(ModelApple ::大);


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)
public:
    enum AppleType {
      Big,
      Small
    }
}

And here is i trying to do:

convertEnumToQString(ModelApple::Big)

Return "Big"

Is this possible? If you have any idea about convertEnumToQString, please share it

解决方案

You need to use Q_ENUM macro, which registers an enum type with the meta-object system.

enum AppleType {
  Big,
  Small
};
Q_ENUM(AppleType)

And now you can use the QMetaEnum class to access meta-data about an enumerator.

QMetaEnum metaEnum = QMetaEnum::fromType<ModelApple::AppleType>();
qDebug() << metaEnum.valueToKey(ModelApple::Big);

这篇关于Qt:如何将枚举转换为QString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆