Get values from nested properties



  • I'm trying to read values from nested properties:

    // root class ...
    class TestRootClass : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
        Q_PROPERTY(TestPropertyObject test READ test WRITE setTest NOTIFY testChanged)
    ...
    }
    
    // class of property in root class
    class TestPropertyObject
    {
        Q_GADGET
        Q_PROPERTY(QString name READ name WRITE setName)
        Q_PROPERTY(int count READ count WRITE setCount)
    ...
    }
    

    I want to read Q_PROPERTY values from any arbitrary object but taking into account the property-hierarchy as far as possible and got this far:

    QMap(
    	("applicationName", QVariant(QString, "applicationName"))
    	("applicationVersion", QVariant(QString, "applicationVersion"))
    	("objectName", QVariant(QString, "objectName"))
    	("organizationDomain", QVariant(QString, "organizationDomain"))
    	("organizationName", QVariant(QString, "organizationName"))
    	("quitLockEnabled", QVariant(QString, "quitLockEnabled"))
    	("name", QVariant(QString, "name"))
    	("test", QVariant(QVariantMap, QMap(
    		("count", QVariant(QString, "count"))
    		("name", QVariant(QString, "name"))
    	)))
    )
    

    The problem is that this QMap is based on staticMetaObject informations and therefor there are no values available.

    Any idea how i can retrieve the values of these nested properties?

    BR


  • Moderators

    Hi! I'm not sure what you want to achieve. You can retrieve every QObject's attached QMetaObject with

    QObject *obj = new TestRootClass;
    QMetaObject *mo = obj->metaObject(); 
    

    You can now access all its properties (with property(index)) and then read the value of this property from your actual object (.read(obj)).

    
    for (int i = mo->propertyOffset(); i < mo->propertyCount(); ++i) {
        QVariant tmp = mo->property(i).read(obj);
        qDebug() << tmp;
    }
    

    Hope it helps!



  • Thanks for your reply.
    The problem is that i can't get a proper object from a property (- just QVariant or void*).
    So how can i access the MetaObject of this property object?


Log in to reply
 

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