Qt 如何分配singleton类型的qmlistProperty

Qt 如何分配singleton类型的qmlistProperty,qt,qml,qqmllistproperty,Qt,Qml,Qqmllistproperty,我已将MyType注册为qmlRegisterSingletonType(“org.examples”,1,0,“MyType”),它具有qmlistproperty-myList。 在QML端,我应该使用什么语法将某些内容分配给MyType.myList 似乎这是不正确的: Component.onCompleted: { MyType.myList = [ MyListElement { } ] } 您必须使用javascript创建MyListElement对象,

我已将
MyType
注册为
qmlRegisterSingletonType(“org.examples”,1,0,“MyType”)
,它具有
qmlistproperty
-
myList
。 在QML端,我应该使用什么语法将某些内容分配给
MyType.myList

似乎这是不正确的:

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

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

在本例中,假设MyListElement是通过以下方式创建的:

qmlRegisterType(“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);
}

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

在本例中,假设MyListElement是通过以下方式创建的:

qmlRegisterType(“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);
}

这是可行的,但这种语法很糟糕。使用Qt.createQmlObject或Qt.createComponent时,无法访问通过id添加到列表中的对象,但这种语法非常糟糕。使用Qt.createQmlObject或Qt.createComponent时,无法访问通过id添加到列表中的对象