Finding Classes By Meta Information

  • Having registered several class types using the qRegisterMetaType function, is there any way I'm able to iterate through these registered classes and check for specific type inheritance?

    I want to build a list of specific classes which derive from a certain base class and then add these into a QListWidget.

    Not sure even where to begin.

  • Qt Champions 2017

    @webzoid said in Finding Classes By Meta Information:

    is there any way I'm able to iterate through these registered classes

    I don't believe so, you should keep that information yourself.

    check for specific type inheritance?

    You can do that. Suppose you have a class X that's registered as a metatype, and you want to check if it's derived from a class Y (also registered as a metatype). Then you can pull the meta-object for that class and check it against the one for class Y. Something along those lines (Qt 5.7+):

    const QMetaObject * xMetaObject = QMetaType::metaObjectForType(qMetaTypeId<X>());
    const QMetaObject * yMetaObject = QMetaType::metaObjectForType(qMetaTypeId<Y>());
    Q_ASSERT(xMetaObject && yMetaObject);
    if (xMetaObject->inherits(yMetaObject))
        ; // X is derived from Y;
        ; // ... it is not

  • @kshegunov Thank you for your comprehensive response.

    The last bit is very useful but its a shame that I can't iterate through registered meta types. I s'pose I could always have my own function which registers the type into a list (maintaining the MetaTypeId for instance as well as calling qRegisterMetaType.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.