Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get mouse coordinates in real time when dragging an element in QML?

I have a program written in Qt, who uses QML.

I'd like to know if it is possible to know the coordinates in real time during the drag of an element.

Actually, I use a custom image that replaces the cursor, its position is updated with the coordinates of the cursor (which is hidden).

I can actually recover the position of the mouse cursor during its deplacement with "onPositionChanged" or just after a click with "onClicked."

I want also to know these coordinates during a drag, I tried with "onPressed" and "onPressAndHold" but without success, the position of the custom cursor is updated only on the click release.

Any help would be appreciated, thank you in advance.

like image 533
Damien Avatar asked Dec 26 '22 11:12

Damien


1 Answers

Dragging elements changes the x and y properties directly so you can monitor those:

Rectangle {

    id: draggable

    width: 100
    height: 100

    onXChanged: {
        if (mouseArea.drag.active) {
            console.log("x=" + x)
        }
    }
    onYChanged: {
        if (mouseArea.drag.active) {
          console.log("y=" + y)
        }
    }

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        drag {
            target: draggable
            axis: Drag.XandYAxis
        }
    }
}
like image 121
sebasgo Avatar answered Dec 29 '22 07:12

sebasgo