QML动态组件创建
我正在尝试通过 Javascript 函数以编程方式在 QML 中创建 QtMobility MapPolyline 的实例和几个 坐标
。
据我所知,从 Javascript 函数创建新实例的唯一方法是使用 Qt.createComponent 和 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);
}
I'm trying to create an instance of a QtMobility MapPolyline
and several of Coordinate
in QML programmaticaly from a Javascript function.
As far as I can see the only way to create new instance from Javascript functions is to use Qt.createComponent
and Qt.createQmlObject
. However, I can't find a way to call createComponent
that will work (I always get a file doesnt exist error). And I'd like to avoid Qt.createQmlObject
since it seems like a really bad practice.
Is there any clean way to achieve this?
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Qt.createComponent 需要一个 URL 来加载文件。这就是为什么它会抱怨文件丢失;您正在尝试加载一个名为“矩形”的文件。
如果您只想动态创建对象,请创建组件声明式。
这样 createPoly 函数应该可以正常工作。
Qt.createComponent needs a URL to load a file from. That's why it complains about a missing file; you're trying to load a file called "Rectangle".
If you just want to create objects dynamically create your component declarative.
That way the createPoly function should work fine.