Co-ordinates for rectItem wrt to other rectItem in qgraphicsScene

75 views Asked by At

In a qgraphicsScene, we have 2 rectItems added.

enter image description here

Let's say, the red rect is added first and it's topleft coordinates are (x1, y1) wrt to the qgraphicsscene. Now a second blue rect is added in the scene, which overlaps the red rect.

Now how can I get the co-ordinates of red rect wrt to the blue rect's co-ordinate system.

No parenting is done on the rectItems, it's only 2 rectItems added in the scene. tried mapRectFromScene and others also, but didn't get results.

2

There are 2 answers

0
Sayan Bera On BEST ANSWER

I got it done by,

x = (red.x - blue.x)

y = (red.y - blue.y)

0
Gabriella Giordano On

You can use a QTransform object to calculate the red rectangle coordinates wrt the blue one as follows:

QRect red, blue; // somewhere in your code...

QTransform t;
t.translate(blue.x(), blue.y()); // move the origin to blue's top left corner
QRect redWRTBlue = t.mapRect(red); // get a copy of red wrt blue's position

redWRTBlue.x() and redWRT.y() is what you want.