简体   繁体   中英

how to assign QmlListProperty of singleton type

I have MyType registered as qmlRegisterSingletonType<MyType>("org.examples", 1, 0, "MyType") which has QmlListProperty<MyListElement> - myList . What syntax i should use at QML side to assign something to MyType.myList ?

Seems like that is incorrect:

Component.onCompleted: {
  MyType.myList = [
    MyListElement {
    }
  ]
}

You have to create the MyListElement object using javascript and then add it to the list with push since QmlListProperty is translated to a list in QML.

In this case with Qt.createQmlObject() , so assuming MyListElement was created with:

qmlRegisterType<MyListElement>("org.examples", 1, 0, "MyListElement");

So the solution is:

Component.onCompleted: {
    var o = Qt.createQmlObject('import org.examples 1.0; MyListElement {}', this)
    // o.foo_property = foo_value;
    MyType.myList.push(o);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM