QtQuick:在Map上拖动MapQuickItem

时间:2017-03-24 05:32:04

标签: qt qml qtquick2 qt-quick

我想实现自我拖曳MapQuickItem。简单的例子:

MapQuickItem {
    id: markerItem

    sourceItem: Rectangle {
        id: sourceRect
        color: "red"
        width: 20
        height: 20
        x: 0
        y: 0

        MouseArea {
            drag.target: markerItem
            cursorShape: drag.active ? Qt.ClosedHandCursor : Qt.OpenHandCursor
            anchors.fill: parent
        }
    }
    Drag.active: true
}

关键是,如果我快速拖动,一旦光标离开标记,拖动就会中断。有没有办法让它正常工作?

1 个答案:

答案 0 :(得分:1)

我找到了一种解决方法:使用单独的可拖动QQuickItem和锚MapQuickItem

MapQuickItem {
    id: anchor
    sourceItem: Item {}
}

Rectangle {
    id: handle

    property bool dragged: mouseArea.drag.active

    color: "red"
    width: 20
    height: 20
    x: anchor.x - width
    y: anchor.y - height

    MouseArea {
        id: mouseArea
        enabled: draggable
        drag.target: handle
        drag.threshold: 0
        anchors.fill: parent
        cursorShape: dragged ? Qt.ClosedHandCursor : Qt.OpenHandCursor
    }

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

    onXChanged: if (dragged) anchor.x = x + width
    onYChanged: if (dragged) anchor.y = y + height

    Drag.active: true
}

使用动态填充的QML Map并不是非常方便,但可以完成工作