Game Programming using Qt 5 Beginner's Guide
上QQ阅读APP看书,第一时间看更新

The scene's coordinate system

Any item can be moved in the scene using the setPos() function. Try to call textItem->setPos(50, 50) and verify that the text was moved in the scene. Technically, this operation changes the transformation between the item's coordinate system and the scene's coordinate system. A convenience function called moveBy() allows you to shift the position by specified amounts.

An item can also be rotated with setRotation() and scaled with setScale(). Try calling textItem->setRotation(20) to see this in action. If you need a more advanced transformation, such as shearing, or you want to perform multiple translations in a particular order, you can create a QTransform object, apply the required transformations, and use the setTransform() function of the item.

The setRotation() function accepts qreal as the argument value, which is usually a typedef for double. The function interprets the number as degrees for a clockwise rotation around the z coordinate. If you set a negative value, a counter-clockwise rotation is performed. Even if it does not make much sense, you can rotate an item by 450 degrees, which will result in a rotation of 90 degrees.