如何在MouseArea中处理MouseArea并在qt qml中拖动MapQuickItem时获取地图上的xy MouseArea实时坐标

问题描述 投票:0回答:1

我想在连续拖动地图上的MapQuickItem的同时获取鼠标区域的坐标。在我的代码中,我将MouseArea放在了MouseArea中。我想要内部的MouseArea坐标。但这给了恒定的价值。如何在MouseArea中处理MouseArea并在qt qml中拖动MapQuickItem时在地图上获取xy MouseArea?

import QtQuick 2.12
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Controls 2.5
import QtQuick.Controls.Styles 1.3
import QtLocation 5.6
import QtPositioning 5.5


Window {
    id: window
    width: 800
    height: 800
    visible: true

    property bool dragged: mouseArea.drag.active

    Plugin
    {
        id: hereMaps
        name: "here"
        PluginParameter { name: "here.app_id"; value: "oBB4FivcP23m2UZQCj8K" }
        PluginParameter { name: "here.token"; value: "P-D8XRRGeVt0YphUuOImeA" }
    }

    Map {
        id: mapOfWorld
        anchors.centerIn: parent;
        anchors.fill: parent
        activeMapType: supportedMapTypes[1];
        zoomLevel: 18
        plugin: hereMaps
        center: QtPositioning.coordinate(19.997454, 73.789803)

        MouseArea{
            id : mapAreaClick
            height: mapOfWorld.height
            width: mapOfWorld.width
            hoverEnabled: true
            anchors.fill: mapOfWorld


        Component{
            id : mapComponent
            MapQuickItem {
                id: anchor
                coordinate: QtPositioning.coordinate(19.997454, 73.789803)
                sourceItem: Item {
                    Rectangle {
                        id: handle
                        color: "red"
                        width: 40
                        height: 40
                        x: anchor.x - width
                        y: anchor.y - height
                        Drag.active: true

                        MouseArea {
                            id: mouseArea
                            drag.target: handle
                            drag.threshold: 0
                            anchors.fill: parent
                        }

                        Connections {
                            target: anchor
                            onXChanged: if (!dragged) x = anchor.x - width
                            onYChanged: if (!dragged) y = anchor.y - height
                        }

                        onXChanged: { console.log("X:"+x)
                            if (dragged) anchor.x = x + width
                        }
                        onYChanged: if (dragged) anchor.y = y + height
                    }
                }
            }
        }

    }
}}
qt drag-and-drop qml maps mouseevent
1个回答
0
投票

我不确定我是否理解您的问题。

为什么要在另一个区域内放置鼠标区域?

也许您需要的是这些活动:

 onMouseXChanged: {
     if (pressed)
        console.log("X:"+mapAreaClick.mouseX);
 }

 onMouseYChanged: {
     if (pressed)
        console.log("Y:"+mapAreaClick.mouseY);
 }

或者如果在拖动项目时需要坐标,则只需检查项目的坐标:

Rectangle {
    ...
    onXChanged: console.log("X:"+x);
    onYChanged: console.log("Y:"+y);
}

如果那不是您的解决方案,请进一步说明您的目标。

© www.soinside.com 2019 - 2024. All rights reserved.