Search code examples
c++qtqmlqtcoreqmetaobject

Enumerate QML properties in Qt C++?


Im doing the following to render a QML in my Qt embedded app:

QDeclarativeView *view = new QDeclarativeView(this);
view->setSource(QUrl::fromLocalFile("dial.qml"));
view->show();
QObject *dial = view->rootObject();

Is there a way i can enumerate all the property values defined in the root item?

For example, if i have the QML:

import QtQuick 1.0
Item {
  id: root
  property real dial_value : 0
  property real dial_length: 0
  property real background_opacity: 1
  etc, etc
}

Is there a Qt method that will end up with me having a list of these strings:

dial_value
dial_length
background_opacity

Ive tried, the following, but it the list is empty:

QList<QByteArray> list = dial->dynamicPropertyNames();

Thanks in advance!


Solution

  • You could write it:

    for (int i = 0; i < dial->metaObject->propertyCount(); ++i) {
        QMetaProperty metaProperty = dial->metaobject()->property(i);
        qDebug() << metaProperty.name();
    }