Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Map MouseArea x, y to global
Forum Updated to NodeBB v4.3 + New Features

Map MouseArea x, y to global

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 859 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bob64
    wrote on last edited by
    #1

    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?

    B 1 Reply Last reply
    0
    • B Bob64

      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?

      B Offline
      B Offline
      Bob64
      wrote on last edited by
      #2

      @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.

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved