Rendering QGraphicsItem into QPixmap
I was in need of rendering a
QGraphicsItem into a
QPixmap outside of the QGraphics framework. Although the final solution is simple, it took me a while to get it right. As Google wasn’t much of a help here I decided to publish my solution to the problem here.
The least obvious part is that we have to take care of rendering the children ourselves. This isn’t that obvious because that is usually taken care of automatically by the QGraphics framework. However, as the goal is to render the
QGraphicsItem outside of the QGraphics framework we have to handle it manually. This also includes taking care of translating between the coordinate systems as each
QGraphicsItem maintains its own coordinate system.
Furthermore, a check has been added to abort the process if the item has either a null or an invalid bounding rectangle. Otherwise the painting operation will fail and throw a warning because there is no surface to paint on:
It is worthwhile to note that we don’t have to use any recursion for painting the children:
QGraphicsItem::childItems() returns a list of all child items (also the children of the children) already sorted in correct Z-Order.
This code has been tested successfully on Qt 5.7. Feel free to use this code for what ever you want.