Important: Please read the Qt Code of Conduct -

QMl "line" can't track the correct mouse position in real time

  • In the process of moving the mouse, I want the X attribute of "line" to change with MouseX at the same time , but when I move the mouse faster, "line" can't track the correct mouse position in real time(there is a interval between "line" and mouseX position) . What can I do to solve this problem (is it the refresh rate problem?)

    import QtQuick 2.14
    import QtQuick.Window 2.14
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
            width: parent.width
            height: 100
            anchors.centerIn: parent
            color: "#131314"
            MouseArea {
                anchors.fill: parent
                hoverEnabled: true
                onMouseXChanged: {
                   aaa.x = area.mouseX
            Rectangle {
                id: line
                width: 1
                height: 100
                x: 0
                anchors.verticalCenter: backgroup.verticalCenter
                color: "yellow"

  • @nature said in QMl "line" can't track the correct mouse position in real time:

    onMouseXChanged: {
    aaa.x = area.mouseX

    what is aaa ? I think this should be line

  • Moderators

    @nature works fine for me, if I replace aaa with line like @KroMignon suggested.

    The only issue happens, when you jank the mouse outside of the window, then of course the line will remain where it is and not move to the border.

    Is that what you mean?

  • @KroMignon Yes I write wrong

  • @J-Hilk you move stop , it works fine。but when you move faster , the interval between line and mouse pointer is exist

  • @J-Hilk E7D909A1-A582-41bb-9B78-0CF27D7CFB86.png
    you can see my screenshot when move faster,

  • The mouse cursor (in my understanding) is coupled to the Windows desktop and Windows display/windowing system in ways that other parts of your application are not.

    I can imagine that the Windows system process responsible for the whole display may be updating the actual cursor faster than the Qt GUI event loop receives (or handles) mouse movement events.

    (Note: this is just guesswork and "gut instinct" and a sprinkling of years-ago lived experience that I am speaking from. If anyone knows better, please correct me.)

    Depending on the ultimate goal of your application, if you have an absolute requirement that a line track EXACTLY to the mouse cursor at all times, then you might need to consider creating a custom cursor in the shape of a line, and telling the Windows system about your custom cursor.

    It might be possible to do this via Qt classes like QCursor:

    Many years ago, I did something like what I just described in an application using eye-tracking. The eye-tracker acted as the mouse, and the only way to guarantee that the on-screen gaze marker was at all times in the same position as the mouse cursor was to make them one and the same: use a custom icon of the on-screen gaze marker to just replace the default arrow cursor system-wide (while the app was foregrounded). I was not using Qt at the time, so I cannot speak with any authority as to whether Qt would have helped me achieve a similar result.

  • @KH-219Design Thank you for your reply. Our products are cross platform, including windows and Mac OS.

Log in to reply