列表中元素的QML访问属性

列表中元素的QML访问属性,qml,qqmlcomponent,Qml,Qqmlcomponent,我有一个MapPolyline列表,并尝试将动态添加的对象保存到此列表中。好吧,它是有效的,但当我尝试获取列表中的对象时,它不起作用。它说TypeError:无法读取未定义的属性这里是我的代码 property list<MapPolyline> lines var component; var sprite; component = Qt.createComponent("lineStringComponent.qml"); sprite = component.crea

我有一个MapPolyline列表,并尝试将动态添加的对象保存到此列表中。好吧,它是有效的,但当我尝试获取列表中的对象时,它不起作用。它说TypeError:无法读取未定义的属性这里是我的代码

property list<MapPolyline> lines

 var component;
 var sprite;
 component = Qt.createComponent("lineStringComponent.qml");
 sprite = component.createObject(window,{"id": "button1"});
 sprite.addCoordinate(QtPositioning.coordinate(currentgcppoint.lat, currentgcppoint.lon));
 sprite.addCoordinate(gcppoint);
 map.addMapItem(sprite)
 lines.push(sprite)
 gcps.push(currentgcppoint)
 console.log("Added:", lines[1])
 console.log("width:", lines[1].line.width)
控制台输出为:

Added: undefined
TypeError: Cannot read property 'line' of undefined

当它试图创建一个新对象时,似乎有一个延迟。我们如何克服这种延迟?

如果我正确地阅读了代码,您只需在行中添加1个元素,然后尝试用行[1]检索行中的第二个元素。这显然是未定义的

尝试访问行为[0]的行的第一个元素。 与大多数语言一样,JS数组的索引以0开头


如果对象创建延迟,那么您就不能更改其任何属性,您可以使用sprite.addCoordinate…

哇,非常感谢这是我的明显错误,我没有注意到,非常感谢
Added: undefined
TypeError: Cannot read property 'line' of undefined