4

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.

2 Answers 2

10

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
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Consider to set "hoverEnabled: true" in MouseArea

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.