Qt: No metadata by meta.enumeratorCount() for enum in Q_OBJECT, why?

我怕爱的太早我们不能终老 提交于 2019-12-02 13:40:16

问题


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 "";
}

回答1:


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);
};


来源:https://stackoverflow.com/questions/11679057/qt-no-metadata-by-meta-enumeratorcount-for-enum-in-q-object-why

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!