我有一个MapPolyline列表,我尝试将动态添加的对象保存到这个列表中。好的,它可以工作,但是当我试图将对象放到列表中时,它就不起作用了。上面写着TypeError: Cannot read property of undefined这是我的代码
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)这是lineStringComponent.qml
import QtQuick 2.2
import QtLocation 5.6
import QtPositioning 5.6
MapPolyline {
id:polyline
line.width: 3
line.color: Qt.rgba(Math.random(),Math.random(),Math.random())
}控制台的输出是:
Added: undefined
TypeError: Cannot read property 'line' of undefined当它试图创建一个新的对象时,它似乎有一个延迟。我们怎样才能克服这种拖延呢?
发布于 2017-10-24 19:54:49
如果我正确地阅读了您的代码,您只会将1元素添加到lines中,而不是尝试用line[1]检索lines的第二个元素。这显然是undefined。
尝试使用lines访问line[0]的第一个元素。
JS数组的索引以0开始(与大多数语言一样)。
如果对象创建出现延迟,则不能更改它的任何属性(sprite.addCoordinate(...))
https://stackoverflow.com/questions/46918930
复制相似问题