Qt:如何将枚举转换为QString [英] Qt: How to convert enum to QString
本文介绍了Qt:如何将枚举转换为QString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图使用Qt反射将枚举转换为QString。
I am trying to use the Qt reflection for converting enum to QString.
这是代码的一部分:
class ModelApple
{
Q_GADGET
Q_ENUMS(AppleType)
public:
enum AppleType {
Big,
Small
}
}
尝试执行:
convertEnumToQString(ModelApple::Big)
返回Big
如果你对 convertEnumToQString
有任何想法,请分享
推荐答案
您需要使用 Q_ENUM 宏,该宏在元对象中注册枚举类型系统。
You need to use Q_ENUM macro, which registers an enum type with the meta-object system.
enum AppleType {
Big,
Small
};
Q_ENUM(AppleType)
现在您可以使用 QMetaEnum 类访问有关枚举器的元数据。
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屋!
查看全文