繁体   English   中英

如何分配单例类型的QmlListProperty

[英]how to assign QmlListProperty of singleton type

我将MyType注册为qmlRegisterSingletonType<MyType>("org.examples", 1, 0, "MyType") ,它具有QmlListProperty<MyListElement> myList 我应该在QML端使用什么语法向MyType.myList分配某些MyType.myList

似乎不正确:

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

由于QmlListProperty已转换为QML中的列表,因此您必须使用javascript创建MyListElement对象,然后使用push将其添加到列表中。

在这种情况下,使用Qt.createQmlObject() ,因此假设MyListElement是通过以下方式创建的:

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

因此解决方案是:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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