Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt QML映射在组件完成时不提供任何坐标_Qt_Qml - Fatal编程技术网

Qt 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 {

我正试图从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 {
    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