How to print a list of custom properties of a QML object?

By creating a QML QtObject:

QtObject {
    property int p: 42
    property int q: 44
}

      

Once you have saved in a local variable QObject *obj

, how to print the names of all the custom properties and possibly value (ie only p

and q

for the above example)? I would like this to work for any class (not just QtObject

), not print properties that have already been declared with Q_PROPERTY

.

Clarification: by "custom" I do not mean properties added via QObject::setProperty

that were not declared with Q_PROPERTY

. I mean properties declared in QML via note property <type> <name>: <value>

that were not declared using Q_PROPERTY

in a subclass QObject

for this QML object. A quick test shows that these properties do not appear in the QObject::dynamicPropertyNames

.

+3


source to share


2 answers


All property information, invokable methods (slots too) and signals are stored by the QMetaObject in each QObject . If you want to list all properties of an object:



QObject *obj = findObjectMethod();
QMetaObject *meta = obj->metaObject();
int n = meta->propertyCount();
for(int i = 0; i < n; ++i)
{
  qDebug() << "Property: " << meta->property(i)->name();
}

      

+2


source


C ++ and QML approach

To print only dynamic property names, you can use the very aptly named dynamicPropertyNames ()QObject

function from from within the calling C ++ function

#include <QGuiApplication>
#include <QtQml>

class Object : public QObject
{
    Q_OBJECT
    Q_PROPERTY(int staticProperty READ staticProperty)
public:
    Object() {
        setProperty("dynamicProperty", 1);
    }

    int staticProperty() const {
        return 0;
    }

    Q_INVOKABLE void printDynamicPropertyNames() const {
        qDebug() << dynamicPropertyNames();
    }
};

int main(int argc, char** argv)
{
    QGuiApplication app(argc, argv);

    Object object;

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("object", &object);
    engine.load("main.qml");

    return app.exec();
}

#include "main.moc"

      

main.qml:

import QtQuick 2.3
import QtQuick.Controls 1.2

ApplicationWindow {
    width: 400
    height: 400
    visible: true

    Component.objectName: object.printDynamicPropertyNames()
}

      

Output:

("dynamicProperty")

      




QML approach only

If you want to print all properties of an object, dynamic or not, exclusively with QML, use the JavaScript for ... in syntax:

The for..in operator iterates over the enumerated properties of an object in no particular order. For each individual property, statements can be executed.

import QtQuick 2.2

Rectangle {
    id: rect
    width: 360
    height: 360

    Component.onCompleted: {
        for (var prop in rect) {
            print(prop += " (" + typeof(rect[prop]) + ") = " + rect[prop]);
        }
    }
}

      

Output:

qml: objectName (string) = 
qml: parent (object) = null
qml: data (object) = [object Object]
qml: resources (object) = [object Object]
qml: children (object) = [object Object]
qml: x (number) = 0
qml: y (number) = 0
qml: z (number) = 0
qml: width (number) = 360
qml: height (number) = 360
qml: opacity (number) = 1
qml: enabled (boolean) = true
...

      

+6


source







All Articles