Finding the coordinates of an item
-
wrote on 23 Mar 2011, 08:48 last edited by
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.
-
wrote on 23 Mar 2011, 08:51 last edited by
Hi Deqing,
You may try to use the mouseX and mouseY properties of the MouseArea object.
Does this work for you?Regards,
Wladek -
wrote on 23 Mar 2011, 19:53 last edited by
@
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 } } }
}
@ -
wrote on 23 Mar 2011, 23:24 last edited by
May i ask why you add 5 to x/y
-
wrote on 23 Mar 2011, 23:29 last edited by
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.
-
wrote on 23 Mar 2011, 23:38 last edited by
Aha ok thanks for the answer.
But if i understand you correctly, its not necessary to add. -
wrote on 24 Mar 2011, 00:50 last edited by
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 -
wrote on 24 Mar 2011, 04:34 last edited by
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?
-
wrote on 24 Mar 2011, 04:45 last edited by
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.
8/9