简体   繁体   English

QML 鼠标在 MouseArea 中的绝对位置

[英]QML mouse absolute position in MouseArea

How to get the absolute mouse position from the mouse area ?如何从鼠标区域获取绝对鼠标位置? I need to have it to show a popup a the correct position我需要让它显示一个弹出窗口的正确位置

Item {
    Menu {
        id: menu
        MenuItem {
            onTriggered: {
               // Need Mouse absolute position
            }
        }
    }
    MouseArea {
        id: mouseArea
        anchors.fill: parent
        onClicked: {
            menu.popup()
        }
    }

You probably found the answer already, but I'll put my solution here for others looking for the same thing.您可能已经找到了答案,但我会将我的解决方案放在这里供其他人寻找相同的东西。

The below function will find the absolute position of the mouse area.下面的函数将找到鼠标区域的绝对位置。 And then you can add mouseX and mouseY accordingly to get mouse position.然后你可以相应地添加 mouseX 和 mouseY 来获取鼠标位置。

Item {
  Menu {
    id: menu
    MenuItem {
      onTriggered: {
        var absolutePos = getAbsolutePosition(source);
        // Need Mouse absolute position
      }
    }
  }
  MouseArea {
    id: mouseArea
    anchors.fill: parent
    onClicked: {
      menu.popup()
    }
  }
  function getAbsolutePosition(node) {
      var returnPos = {};
      returnPos.x = 0;
      returnPos.y = 0;
      if(node !== undefined && node !== null) {
          var parentValue = getAbsolutePosition(node.parent);
          returnPos.x = parentValue.x + node.x;
          returnPos.y = parentValue.y + node.y;
      }
      return returnPos;
  }
}

Short answer简答

    onClicked: {
        var positionInPopup = mapToItem(popup, mouse.x, mouse.y)
    }

Longer answer更长的答案

Like hinted by indalive, the preferred method for mapping coordinates is by using mapToItem , available for any Item.就像 indalive 暗示的那样,映射坐标的首选方法是使用mapToItem ,可用于任何项目。 It transforms coordinates (and size) from current Item coordinates system (if not specified otherwise) to another Item coordinates system.它将坐标(和大小)从当前项目坐标系统(如果没有另外指定)转换为另一个项目坐标系统。 And the mapFromItem counterpart does the reverse, naturally.mapFromItem对应物自然而然地进行相反的操作。

From Qt 5.7, you also havemapToGlobal , which will give you coordinates in the system/screen referential.从 Qt 5.7 开始,您还有mapToGlobal ,它将为您提供系统/屏幕参考中的坐标。

MouseArea {

    // ...

    onPositionChanged: {
        var positionInRoot = mapToItem(root, mouse.x, mouse.y)
        var positionInWindow = mapToItem(window.contentItem, mouse.x, mouse.y)
        var globalPosition = mapToGlobal(mouse.x, mouse.y)

        console.log("For root: " + positionInRoot )
        console.log("For window: " + positionInWindow)
        console.log("For system: " + globalPosition)
    }
}

Given the example above, and ...鉴于上面的例子,和...

  • your MouseArea is close to root , a bit further from your Window top left corner你的MouseArea靠近root ,离你的Window左上角有点远
  • the Window itself is 1000px+ from the leftmost of your screen(s)窗口本身距离屏幕最左侧 1000px+

... you will see: ……你会看到:

For root: QPointF(10, 0)对于根:QPointF(10, 0)

For window: QPointF(150, 100)对于窗口:QPointF(150, 100)

For system: QPointF(1230, 120)对于系统:QPointF(1230, 120)

Caveat with Window type Window类型的警告

When converting to/from a Window (QML type), you need to use its contentItem property, as mapTo/From only work with Item s.在与Window (QML 类型)之间转换时,您需要使用其contentItem属性,因为 mapTo/From 仅适用于Item

In this case mouseArea fills his parent (anchors.fill: parent), therefore mouseArea.mouseX and mouseArea.mouseY are absolute mouse position.在这种情况下 mouseArea 填充他的父级(anchors.fill: parent),因此 mouseArea.mouseX 和 mouseArea.mouseY 是绝对鼠标位置。 For relative positions you should use mapFromItem and mapToItem functions http://doc.qt.io/qt-5/qml-qtquick-item.html#mapToItem-method对于相对位置,您应该使用 mapFromItem 和 mapToItem 函数http://doc.qt.io/qt-5/qml-qtquick-item.html#mapToItem-method

If you check the documentation for the onClicked signal in Mouse Area( http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-mousearea.html#onClicked-signal ) you are given a MouseEvent param named mouse.如果您检查鼠标区域中 onClicked 信号的文档( http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-mousearea.html#onClicked-signal ),您将获得一个名为的 MouseEvent 参数鼠标。 Using the MouseEvent object( http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-mouseevent.html ) you can access the mouse positions inside the onClick handler with使用 MouseEvent 对象( http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-mouseevent.html ),您可以访问 onClick 处理程序内的鼠标位置

mouse.x
mouse.y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM