本文介绍了Qt:没有元数据meta.enumeratorCount()对于Q_OBJECT中的枚举,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下类,其中我尝试获取一些枚举MyEnum
的元数据。然而,当循环 meta.enumeratorCount()
它的计数总是0.基本上我是以下。为了找到问题,我试着和方法一样,同样的问题 - 方法计数0.代码编译,没有错误没有警告。
I have the following class, where I try to obtain some metadata of an enum MyEnum
. However, when looping over meta.enumeratorCount()
its count is always 0. Basically I was follwing this example here. In order to find the problem, I was trying the same with methods too, same problem - method count 0. Code compiles, no errors no warnings.
必须是一个愚蠢的错误....也许你可以帮助我
Must be a silly mistake .... maybe you can help me
class FsxSimConnectQtfier : public QObject
{
Q_OBJECT
public:
explicit FsxSimConnectQtfier(QObject *parent = 0);
enum MyEnum { G1, G2 };
static const QString simConnectExceptionToString(const DWORD id);
};
const QString FsxSimConnectQtfier::simConnectExceptionToString(const DWORD id) {
// int i= FsxSimConnectQtfier::staticMetaObject.indexOfEnumerator("MyEnum");
// -1 -> not found, why?
QMetaObject meta = FsxSimConnectQtfier::staticMetaObject;
for (int i=0; i < meta.enumeratorCount(); ++i) {
QMetaEnum m = meta.enumerator(i); // never reached, why?
}
return "";
}
推荐答案
与元数据系统使用:
You need to register the enum with the metadata system using the Q_ENUMS()
macro:
class FsxSimConnectQtfier : public QObject
{
Q_OBJECT
Q_ENUMS(MyEnum) // <---
public:
explicit FsxSimConnectQtfier(QObject *parent = 0);
enum MyEnum { G1, G2 };
static const QString simConnectExceptionToString(const unsigned int id);
};
这篇关于Qt:没有元数据meta.enumeratorCount()对于Q_OBJECT中的枚举,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!