简体   繁体   中英

How to get names of the struct members from QMetaType?

I have:

struct myStruct {
    int m_valueA;
    float m_valueA;
    bool m_valueA;
};
Q_DECLARE_METATYPE(myStruct)

....

struct myStructXY {
    ....
};
Q_DECLARE_METATYPE(myStructXY)

I can get enum names from QMetaEnum :

Q_ENUM(myEnum)
QMetaEnum enumTypes QMetaEnum::fromType<myEnum>();

QMap<qint32, QString> labelsMap;
for(int i = 0; i < enumTypes.keyCount(); i++)
{
    QString enumName = QString(enumTypes.key(i));
    labelsMap.insert(enumTypes.value(i), enumName);
}

I would like to dynamicaly collect the myStruct members names and types from metatype similar as in previuos case of Enums. Like:

QMetaObject structType;
QStringList list = getStructMemberNames(structType);

Does Qt have an option to do this? I have searched the QMetaType but I need more power to do this.

I do not think there are any mean to do this in Qt.

The closest thing would be QMetaObject which exposes signals, slots and properties; properties could be seen like exposing member variables, but that is not exactly what you asked.

Also note that QMetaEnum works only if you used Q_ENUM in a QObject class processed by the moc. So for making something similar with a struct it would work only with struct inside QObjects anddeclared with a Q_STRUCT, but as of now this does not exist.

How to get names of struct members from QMetaType?

Not using Qt to begin with, since moc does not support generating such metadata. That's all. If you wish, you could patch moc to do it for you, but out of the box it doesn't work. You would probably have more success by getting the AST from clang , generating some metadata records from it, and adding it to your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM