Finding the coordinates of an item
-
Is it possible to get the coordinates of an item in the application's coordinate system?
For example if I click on an item, I can get it's current x and y.
-
Hi Deqing,
You may try to use the mouseX and mouseY properties of the MouseArea object.
Does this work for you?Regards,
Wladek -
@
import QtQuick 1.0
Item {
width: 100
height: 200Rectangle { x: 10 y: 20 color: "blue" width: 10 height: 10 MouseArea { anchors.fill: parent onClicked: { console.log("pos " + parent.x + "," + parent.y) parent.x += 5 parent.y += 5 } } }
}
@ -
May i ask why you add 5 to x/y
-
Why not? :)
To show that it displays the current values of x and y, rather than only the starting values. Click on it multiple times and the values change.
-
Aha ok thanks for the answer.
But if i understand you correctly, its not necessary to add. -
You can use Item's mapToItem function to convert local coordinates to view coordinates (http://doc.qt.nokia.com/4.7/qml-item.html#mapToItem-method).
Regards,
Michael -
I'd like to get the x and y of the application's coordinates.
Say a flickable item containing a textinput. The text input can be either at the bottom of the screen, or at the top of the screen if user flick it up. These two different situation should get different y.
Maybe Michael is right, but only works in C++ code?
-
Just found that mapToItem works, as follows:
@var map = textinput.mapToItem(root, 0, 0)@
then map.x map.y is the mapped coordinates of the root element.