Archived:How to apply transformation matrix to graphics item
This article shows how to use Transformation marix to graphics item.
To apply a transform matrix is pretty easy, but when dealing with GraphicsItems in the same scene, you need to take more care of the transformations applied to the item's coordinate system to avoid undesired behaviors.
The video above shows an application with 2 graphic items: the red dot created with QGraphicsScene::addEllipse and a custom graphicsItem which makes use of QTransform. The custom graphics item re-implements the QGraphicsViewItem::paint method in order to draw itself in a "transformed" coordinate system. To keep the code simple and generic, QTransform matrix has not been modified and it's an identity matrix, as defined by the QTransform constructor.
As you can see from the video, at the beginning everything seems to work fine and the red dot is on the top-left corner of the custom item (white box), but when the window is resized (which happens on the mobile devices when the application changes orientation mode) the world coordinate system changes. In the above snippet, QPainter::setTransform overrides the world matrix which prevents the translation of the white box.
This underhand issue is pretty popular but it's not described elsewhere and this is why this Wiki page has been written. This problem happens because the setTransform method overwrites the world matrix by default whereas, in most cases, we want to combine our matrix with the world's one. The following code shows how to combine the two matrices.