我试图从Qt C代码更新MapQuickItem,调试控制台输出看起来很好,但GUI上的Map没有更新。我在QML中有一个MouseArea onClicked来测试相同的QML方法,它工作得很好,新的项目被添加到Map和重新中心。可能出了什么问题?
下面是C代码:
mainwindow.cpp:
QQmlEngine engine;
QQmlComponent component(&engine, "map.qml");
QObject *mapObject = component.create();
QVariant arg1 = 1;
QVariant lat = 10.123465;
QVariant lon = 10.123456;
QVariant int = 50;
QMetaObject::invokeMethod(mapObject, "updateDrone",
Q_RETURN_ARG(QVariant, reVal),
Q_ARG(QVariant, arg1),
Q_ARG(QVariant, lat),
Q_ARG(QVariant, lon),
Q_ARG(QVariant, bearing));
map.qml:
Rectangle {
id: window
property Component droneMaker: droneMarker
property var drone: undefined
property double centerLat: 32.848168
property double centerLon: -111.487827
Plugin {
id: mapPlugin
name: "osm"
}
Map {
id: mapView
anchors.fill: parent
plugin: mapPlugin
center: QtPositioning.coordinate(centerLat, centerLon)
zoomLevel: 13
}
function updateDrone(droneNum: int, currLat: double, currLon: double, bearing: int) {
console.log("updateDrone called");
if (drone !== undefined)
mapView.removeMapItem(drone);
addDrone(droneNum, currLat, currLon, bearing);
if (autoUpdate && centerDrone === droneNum) {
console.log("panning");
mapView.pan(centerLat - currLat, centerLon - currLon);
centerLat = currLat;
centerLon = currLon;
}
}
function addDrone(droneNum: int, currLat: double, currLon: double, bearing: int) {
console.log("addDrone called")
drone = droneMaker.createObject(window,
{coordinate: QtPositioning.coordinate(currLat, currLon),
bearing: bearing});
mapView.addMapItem(drone)
console.log(drone.bearing)
console.log(drone.coordinate)
console.log("drone added")
}
Component {
id: droneMarker
MapQuickItem {
id: drone
property int bearing: 0
anchorPoint.x: droneImage.width/2
anchorPoint.y: droneImage.height/2
coordinate: position
sourceItem: Image {
id: droneImage
source: "drone.png"
}
}
}
MouseArea {
anchors.fill: parent
onClicked: {
var coordinate = mapView.toCoordinate(Qt.point(mouse.x,mouse.y))
console.log("clicked")
updateDrone(1, coordinate.latitude, coordinate.longitude, 45);
}
}
}
下面是从C++调用该方法时的控制台输出:enter image description here
以下是通过鼠标单击调用方法时的控制台输出:enter image description here
1条答案
按热度按时间cygmwpex1#
原来我是先通过引擎加载了我的QML,然后再通过quickwidget加载。
我删除了引擎代码,并使用此代码来代替,现在一切正常: