我正在尝试从 Javascript 函数以编程方式创建 QtMobilityMapPolyline
和 QML 中的几个实例。Coordinate
据我所知,从 Javascript 函数创建新实例的唯一方法是使用Qt.createComponent
and Qt.createQmlObject
。但是,我找不到一种可以调用的方法createComponent
(我总是得到一个文件不存在错误)。我想避免Qt.createQmlObject
,因为这似乎是一种非常糟糕的做法。
有什么干净的方法可以实现这一目标吗?
Component {
id: polyGenerator
MapPolyline {}
}
function addPoly() {
//This next line works, but crashes when trying to add positions
//createPoly(polyGenerator);
var component = Qt.createComponent("Rectangle");
console.log(component.status + " " + Component.Null);
if (component.status == Component.Ready) {
createPoly(component);
} else if (component.status == Component.Error) {
console.log("Error: " + component.errorString());
} else {
component.statusChanged.connect(function () {
if (component.status == Component.Error) {
console.log("Error: " + component.errorString());
return;
}
createPoly(component);
});
}
}
function createPoly(component) {
var poly = component.createObject(map);
poly.border.color = "red";
poly.border.width = 4;
// I get a crash here, my guess is that I need proper Coordinate objects
poly.addCoordinate({latitude: -34.60553, longitude: -58.38088});
poly.addCoordinate({latitude: -34.60720, longitude: -58.38081});
poly.addCoordinate({latitude: 34.60720, longitude: -58.38081});
poly.addCoordinate({latitude: -34.60597, longitude: -58.37930});
map.addMapObject(poly);
}