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. 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.
From Qt 5.7, you also have mapToGlobal
, which will give you coordinates in the system/screen referential.
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
- the Window itself is 1000px+ from the leftmost of your screen(s)
... you will see:
For root: QPointF(10, 0)
For window: QPointF(150, 100)
For system: QPointF(1230, 120)
Caveat with Window
type
When converting to/from a Window
(QML type), you need to use its contentItem
property, as mapTo/From only work with Item
s.