Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Map MouseArea x, y to global



  • I am obviously missing something basic here, but I am struggling to map mouse.x, mouse.y in a MouseArea to the global (main window) coordinates.

    import QtQuick 2.9
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 640; height: 480
        Rectangle {
            width: 250; height: 30
            anchors.centerIn: parent
            border.color: "black"
            MouseArea {
                anchors.fill: parent
                acceptedButtons: Qt.LeftButton
                onClicked: {
                    console.log("mouse x, y = ", mouse.x, mouse.y);
                    var globXY = mapToGlobal(mouse.x, mouse.y);
                    console.log("mouse -> global x, y = ", globXY.x, globXY.y);
                }
            }
        }
    }
    

    Output:

    qml: mouse x, y =  95 11
    qml: mouse -> global x, y =  930 516
    

    What am I missing?



  • @Bob64 D'oh! The clue was in my question. I was assuming global meant relative to main window - it means relative to screen. The solution is to offset by the main window x, y coordinates.



  • @Bob64 D'oh! The clue was in my question. I was assuming global meant relative to main window - it means relative to screen. The solution is to offset by the main window x, y coordinates.


Log in to reply