qtqmlqtquick2qt-quick

QtQuick: dragging MapQuickItem on a Map


I want to achieve self-draggable MapQuickItem. Simple example:

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
}

The point is, if I drag fast, dragging is interrupted as soon as cursor leaves marker. Is there a way to make it work properly?


Solution

  • I have found a workaround: using separate draggable QQuickItem and an anchor 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
    }
    

    It's not super convenient with dynamically populated QML Map, but does the job