Saving a scene to an image file

We've only displayed our scene in the view so far, but it is also possible to render it to an image, a printer, or any other object Qt can use for painting. Let's save our scene to a PNG file:

QRect rect = scene.sceneRect().toAlignedRect();
QImage image(rect.size(), QImage::Format_ARGB32);
image.fill(Qt::transparent);
QPainter painter(&image);
scene.render(&painter);
image.save("scene.png");