Origin point of the transformation

In our next example, we will create a cross at (0, 0) point and add a rectangle to the scene:

You can do it with the following code:

scene.addLine(-100, 0, 100, 0);
scene.addLine(0, -100, 0, 100);
QGraphicsRectItem* rectItem = scene.addRect(50, 50, 50, 50);

In this code, we use the addLine() and addRect() convenience functions. This is the same as creating a QGraphicsLineItem or QGraphicsRectItem and adding it to the scene manually.

Now, imagine that you want to rotate the rectangle by 45 degrees to produce the following result:

A straightforward attempt to do it will use the setRotation() method:

QGraphicsRectItem* rectItem = scene.addRect(50, 50, 50, 50);
rectItem->setRotation(45);

However, if you try to do that, you will get the following result: