Qt QML映射在组件完成时不提供任何坐标
我正试图从QML组件Qt QML映射在组件完成时不提供任何坐标,qt,qml,Qt,Qml,我正试图从QML组件Map中获取两个坐标。我尝试使用标准的组件。未完成的。我试图得到左上方的坐标和右下方的坐标 我正在使用Map函数来协调与Qt.Point()作为参数 调用函数时会出现问题,因为此函数的输出为空 我使用的是Qt5.12.3 输出: qml: qml: 我的代码 import QtQuick 2.12 import QtQuick.Window 2.12 import QtLocation 5.9 import QtPositioning 5.3 Window {
Map
中获取两个坐标。我尝试使用标准的组件。未完成的。我试图得到左上方的坐标和右下方的坐标
我正在使用Map
函数来协调与Qt.Point()
作为参数
调用函数时会出现问题,因为此函数的输出为空
我使用的是Qt5.12.3
输出:
qml:
qml:
我的代码
import QtQuick 2.12
import QtQuick.Window 2.12
import QtLocation 5.9
import QtPositioning 5.3
Window {
visible: true
width: 640
height: 480
Map {
id: map
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
height: parent.height + 20
plugin: mapPlugin
gesture.acceptedGestures: {
MapGestureArea.PanGesture |
MapGestureArea.PinchGesture
}
Component.onCompleted: {
console.log(map.toCoordinate(Qt.point(0,0)))
console.log(map.toCoordinate(Qt.point(map.width, map.height)))
}
}
Plugin {
id: mapPlugin
name: "osm"
PluginParameter {
name: "osm.mapping.cache.directory"
value: "./cache/"
}
}
}
这个问题有什么解决办法吗?有人有类似的问题吗
谢谢你的帮助
//编辑:
qml:
qml:
当我使用map.toCoordinate(Qt.point(0,0))
在map.onCenterChanged
中进行协调,然后使用map
移动时,它返回有效的坐标。如前所述:
mapReady:bool
此属性保存映射是否已成功初始化
并且随时可以使用。一些方法,如FromCoordination和
为了协调,在地图准备好之前将不起作用由于
地图的架构,建议使用发出的信号
此属性取代Component.onCompleted,以确保
一切正常。
(重点是我的)
Component.onCompleted不保证映射已呈现或具有API提供的信息,相反,您必须使用mapReady属性:
Map {
id: map
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
height: parent.height + 20
plugin: mapPlugin
gesture.acceptedGestures: {
MapGestureArea.PanGesture |
MapGestureArea.PinchGesture
}
onMapReadyChanged: {
if(mapReady){
console.log(map.toCoordinate(Qt.point(0,0)))
console.log(map.toCoordinate(Qt.point(map.width, map.height)))
}
}
}
输出:
qml: 51° 32' 29.3" N, 1° 53' 7.8" W, 0m
qml: 51° 28' 23.1" N, 1° 37' 48.4" E, 0m
map.toCoordinate(Qt.point(0,0))
是否返回有效的coordinate
?@SilvanoCerza不在组件中。未完成的控制台.log(map.width,map.height)
返回什么?在组件中,未完成的返回qml:0 20
,wich也是错误的。这是因为您的窗口
大小仍然是0,0
,贴图
是0,20,因为您将20添加到父.height
。