Important: Please read the Qt Code of Conduct -

How to track the global position of an Item?

  • Hi!

    I would like to track the global position of an item (relative to the root item). Property binding to the item's x and y coordinates does not help because they are relative to its parent, so mapToItem would not be reevaluated if the parent is moved.

    Maybe there's something in Qml that I've missed, so before I start working on a C++ extension that provides the information (should be possible, I guess), I'm asking here.



  • Hi Nils,

    I don't think you are missing anything: the built-in QML elements don't provide any simple ways of dealing with global position changes.


  • ok, thanks. I'll look into doing this from the C++ side.

  • Hi Njeisecke,
    I think you really miss something, I use mapFromItem()/mapToItem() function to get the item's position in global scene.

    The document says:
    Maps the point (x, y), which is in item's coordinate system, to this item's coordinate system, and returns an object with x and y properties matching the mapped cooordinate.
    If item is a null value, this maps the point from the coordinate system of the root QML view.

  • Hi diro,

    the problem is that x and y do not change if for example the item's parent is being moved. So in this case mapFromItem would not be reevaluated event though the global position changes.


  • Hi Nils,
    I think the event issue could be solved in QML, the main idea is that if the itme's coordinate won't change but its parent will, how about leverage its notify?

    The original code may be:
    subpanel.mapToItem(null, 0, 0).x
    than, becomes:
    subpanel.mapToItem(null, (basepanel.width * 0) + 0, 0).x

    PS.The basepanel.width may changed to other properties, such as x, y, height... and so on, depends on your case

    The following is my full sample code, you may try it.

    @import QtQuick 1.1

    Rectangle {
    id: basepanel
    anchors.leftMargin: 10;
    anchors.rightMargin: 10;
    height: 480;

    Rectangle {
        id: subpanel
        width: 657
        height: 294
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
        border.color: "#777777"
    Text {
        id: status
        x: parent.width/2
        y: parent.height/2
        color: "#ffffff"
        text: qsTr("Global Position of item subpanel:") +
              subpanel.mapToItem(null, (basepanel.width * 0) + 0, 0).x +
              "," +
              subpanel.mapToItem(null, (basepanel.width * 0) + 0, 0).y;
        visible: true
        styleColor: "#a23e3e"


    Besides, if you really want to monitor many properties of basepanel, you may use
    @ (basepanel.width * basepanel.height * basepanel.x * basepanel.y * 0)@

  • Hi Diro,

    thank you very much for posting your example code.

    The problem is that "subpanel" is supposed to be a component. It doesn't know if the moving item is it's parent, parent parent oder parent parent parent, ...

    So to get it right you would have to iterate through the parent chain and bind to each x and y property. You also have to take possible reparenting into account.

    I guess you could do this in pure Qml. However I doubt that the amount of necessary bindings and function calls would give reasonable performance in more complex applications.

    So as soon as time admits, I'm going to check what could be done from the C++ side. The downside of this approach might be that I'll have to do it again for the scene graph.


  • Hi Nils,
    Okay, I understand your problem now. And I also interested in your C++ solution, hope you can share with us in the future, thanks :-)

  • Hi,

    Did you get any progress on that ?


  • No, not really.

    I've been talking about this with some Qt developers at DevDays and they came to the conclusion that this would be pretty difficult to implement generally without sacrificing overall system performance.

    You should be able to connect to all xChanged and yChanged signals of the item's parent chain but I have not tried this yet. Seems to be the only possibility though.


  • Yes, connecting an item's own x/y-Changed signals to the xChanged and yChanged signals of all parents is possible - I tried it and it works fine. But I do not recommend it for items that change often, because the performance penalty is significant! If you have control over the items you want to track, it would be the best solution to write a custom QDeclarativeItem in C++ and overwrite the itemChange function from QGraphicsItem: "QGraphicsItem itemChange function":

    Regarding the doc, this also has a performance impact, but presumably not that much compared to the parent-signal-connection approach.


  • Maybe we need a specific Item to provide this functionnality or add a new property in item to add tracking global pos.

    For now I used the flag ItemSendsScenePositionChanges in a new item. My purpose is to anchor a punctual item to another punctual item anywhere ni the QML tree here what I used :

    @class AnchorPoint : public QDeclarativeItem
    Q_PROPERTY(AnchorPoint* anchorTo READ anchorTo WRITE setAnchorTo NOTIFY anchorToChanged)

    AnchorPoint(QDeclarativeItem *parent = 0)
    : QDeclarativeItem(parent)
    , m_anchorTo(NULL)
    setFlag(ItemSendsScenePositionChanges, true);

    AnchorPoint* anchorTo() const { return m_anchorTo;}
    void setAnchorTo(AnchorPoint* anchorPoint)
        if(anchorPoint != m_anchorTo)
            if(m_anchorTo != NULL)
            m_anchorTo = anchorPoint;
            if(m_anchorTo != NULL)
    QVariant itemChange(GraphicsItemChange change, const QVariant &value)
        if (change == ItemScenePositionHasChanged && scene())
            emit scenePosChanged();
        return QGraphicsItem::itemChange(change, value);

    void anchorToChanged();
    void scenePosChanged();

    protected slots:
    void anchorPosChanged()
    QGraphicsItem* anchorParent = (QGraphicsItem*)m_anchorTo->parentItem();
    qDebug() << "Item anchored to has no parent : can't anchored to it";

            qDebug() << " Item has no parent : can't be anchored ";
        QPointF anchorPos = m_anchorTo->pos();
        setPos(anchorParent->mapToItem((const QGraphicsItem*)parentItem(),anchorPos));

    AnchorPoint* m_anchorTo;

Log in to reply