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* ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.