[Interest] mapToItem usage and behaviour

Jb Hubert jb.hubert at dim3.com
Mon Feb 9 13:18:03 CET 2015


On Mon, 9 Feb 2015 11:39:06 +0100
Federico Buti <bacarozzo at gmail.com> wrote:

> Hi all.
> 
> Reading the documentation for mapToItem/mapFromItem functions I've
> understand that it is possibile to translate a point to/from the
> coordinate system of the item itself. In particular, given the
> signature:
> 
> <itemName>.mapToItem(null, 0, 0)
> 
> 
> it is possibile to map the coordinate system origin of the item to the
> coordinate system of the top level window.
> 
> 
> This seems to work, as long as I don't use a Window, i.e. the
> following example gives different results (or correct ones which I
> cannot understand?):
> 
> 
> import QtQuick 2.4
> 
> import QtQuick.Window 2.2
> 
> 
> Window {
> 
>     visible: true
> 
>     width: 600
> 
>     height: 600
> 
>     Rectangle {
> 
>         id: rec1
> 
>         x: 200
> 
>         y: 200
> 
>         width: 200
> 
>         height: 200
> 
>         color: "green"
> 
>         opacity: 0.5
> 
>         Component.onCompleted: {
> 
>             var coords = rec1.mapToItem(null, 0, 0)
> 
>             console.log("rec1: " + coords.x + "," + coords.y)
> 
>         }
> 
>     }
> 
>     Rectangle {
> 
>         id: rec2
> 
>         anchors.centerIn: parent
> 
>         width: 200
> 
>         height: 200
> 
>         color: "blue"
> 
>         opacity: 0.5
> 
>         Component.onCompleted: {
> 
>             var coords = rec2.mapToItem(null, 0, 0)
> 
>             console.log("rec2: " + coords.x + "," + coords.y)
> 
>         }
> 
>     }
> 
> }
> 
> 
> The resulting coordinates are:
> 
> 
> qml: rec2: -100,-100
> 
> qml: rec1: 200,200
> 
> 
> whereas I expected both rectangles to be at (200, 200). Window is not
> an Item, I know. Is this behaviour correct and I'm missing something
> trivial? Can the coordinates be traslated as expected?
> 
> 
> Thanks in advance,
> 
> F.

Hello,

I noticed that you use an anchor for rect2. And for what I see, the (x,
y) of your Rectangle have not been yet setted by the layout management
when you pass through the onCompleted handler. If you listen on
on[XY]Changed, you will see that rect2 are first set to (-100, -100)
then position at (200, 200), giving you the expected result.

Hope that helps a bit,
Jean-BenoƮt



More information about the Interest mailing list