Enumerate QML properties in Qt C++?



  • Greetinsg all,

    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!


  • Moderators

    The "rootObject()" is not your "root" item!

    Best way (in my opinion) is to add objectName to your root object:
    @
    Item {
    objectName: "myRoot"
    id: root
    ...
    }
    @

    Then, in C++, you can get that object using QObject::findChildren() or findChild():
    @
    QObject *dial = view->finChild<QObject *>("myRoot");
    @

    Then you can iterate over every property that object has using QObject::metaObject().



  • Oh, nice!

    Thankyou :)


Log in to reply
 

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