Question

I am having problems getting coordinate mapping to work as intended. For some reason the result is usually off, unless a very specific condition is met.

Here is an example scenario:

enter image description here

Objects are ordered in a tree, with each node being 100 pixels square. Mapping is done from each node to the parent of the first node. Items are paranted as they are ordered, e.g. 1 is parent of 2 and 4, 2 is parent of 3 and so on...

  • Node0 is at local coordinates 0,0 in its parent, so logically, mapping the position of node 0 returns 0,0
  • Node1 however does not return 100, 100, instead it returns 200, 200
  • Node2 and 3 behave differently, this time the values are correctly incremented from the previous (incorrect) node value to 300, 300 and 400, 400 respectively
  • Node4 returns 300, 700 even though it it is only 100 pixels below node 2
  • Node5 - 200, 1000
  • Node6 - 300, 700 - it is lower than node 5 but shows a lower y value
  • Node7 - 200, 1400 - 100 pixel lower than node 6 returns 700 additional pixels y value

It seems like this mapping doesn't do what I assume it does, which is produce a coordinate, absolute to the object being mapped to, e.g. the top left corner of Node 0. Which should produce the expected values:

  • Node0 0, 0
  • Node1 100, 100
  • Node2 200, 200
  • Node3 300, 300
  • Node4 200, 400
  • Node5 100, 500
  • Node6 200, 600
  • Node7 100, 700

It seems that every new child at a given level skews the result off. Any idea what is going on here?

Was it helpful?

Solution

After investigating the mapToItem method I concluded it is not the right tool for the job, so I wrote my own.

QPointF absolutePosition() {
        QPointF p(0, 0);
        QQuickItem * item = this;
        while (item != Object::_rootUI) { // absolute position relative to _rootUI
            p += item->position();
            item = item->parentItem();
        }
        return p;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top