c++qtqproperty

List/Get Q_PROPERTY's of derived class only


Imagine a QObject derived class:

class MyObject : public QObject
{
    Q_OBJECT;
    Q_PROPERTY(bool myBool READ myBool WRITE setMyBool);
    //...
}

How do I receive all the properties of the derived class MyObject only without any of the base classes?


Solution

  • Using this code snippet from the Qt documentation, one can list the properties of the derived class only:

    const QMetaObject* metaObject = myObj->metaObject();
    QStringList properties;
    for(int i = metaObject->propertyOffset(); i < metaObject->propertyCount(); ++i)
        properties << QString::fromLatin1(metaObject->property(i).name());
    

    This should work with Qt 4, 5 and 6. Tested with Qt 4.8.