O'Reilly logo

Game Programming using Qt 5 Beginner's Guide - Second Edition by Lorenz Haas, Witold Wysota, Pavel Strakhov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required