![Game Programming using Qt 5 Beginner's Guide](https://wfqqreader-1252317822.image.myqcloud.com/cover/918/36699918/b_36699918.jpg)
上QQ阅读APP看书,第一时间看更新
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:
![](https://epubservercos.yuewen.com/9CD4B3/19470394001568606/epubprivate/OEBPS/Images/29c10c5a-6843-4284-88d8-08444454723b.png?sign=1738864120-QutkAAA39lTaYJJrCmBOz636Zct8B883-0-e0f4bf4b85e7b0dc5441b7cb2d917497)
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:
![](https://epubservercos.yuewen.com/9CD4B3/19470394001568606/epubprivate/OEBPS/Images/b9f23d56-4ce9-44ac-9718-77d4cfb7be27.png?sign=1738864120-nFbu1JVj0Xn6ofiL1wlz1Do7ESO6sd0o-0-3686b4256e27745a18c970a7468d1eeb)
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:
![](https://epubservercos.yuewen.com/9CD4B3/19470394001568606/epubprivate/OEBPS/Images/de15be54-8700-4b6f-beb0-8a9516a4baf2.png?sign=1738864120-of7Xey6HLzYbStKBYPmpvlz0ZmW4pO4D-0-6704f6637562f838e38c3654fecb9034)