3
votes

I have a custom object in QML which uses a Loader to dynamicly loads qml objects and sets properties of it. I want to access all the properties of the ListElement, and pass it trough to the Loader.

model: ListModel {
    ListElement {           
        bodySource: "some_object.qml"
        bodyModel: ListElement {
            myProp1: false
            myProp2: ""
            myProp3: 0.0
        }
    }
}

var bodyModelElement = model.get(0).bodyModel.get(0);
for (var propertyName in bodyModelElement)
{
    console.info("propertyName: " + propertyName);
}

The propblem with this is that I get also a lot of other property names I didn't expect.

qml: propertyName: objectName
qml: propertyName: myProp3
qml: propertyName: myProp2
qml: propertyName: myProp1
qml: propertyName: objectNameChanged
qml: propertyName: __0
qml: propertyName: __1
qml: propertyName: __2

Where __0, __1, __2 and objectNameChanged is of type object and objectName is an empty string

How can I only get myProp1, myProp2 and myProp3 ? Or should I just exclude the names starting with an underscore and objectName(Changed)

1
Not sure it works with such an object, but maybe if(bodyModelElement.hasOwnProperty(propertyName)) can help to filter to undesired properties out.skypjack

1 Answers

3
votes

ListElement operates with roles, not with properties.

From the documentation

... QML elements contain a collection of role definitions instead of properties...

So since these elements can define some properties for their own needs you cannot reference to properties or loop through in such way.

Btw, ListElement in C++ code is just a QObject.

On the other hand, the necessity to loop through roles points to some error in code design since the only purpose of ListElement is defining a pair role_name-value.

If you still want to define some enumerated values within ListElement you can define it as a property - array of values.