What just happened?

First, you determined the rectangle of the scene with sceneRect(). Since this returns a QRectF parameter and QImage can only handle QRect, you transformed it on the fly by calling toAlignedRect(). The difference between the toRect() function and toAlignedRect() is that the former rounds to the nearest integer, which may result in a smaller rectangle, whereas the latter expands to the smallest possible rectangle containing the original QRectF parameter.

Then, you created a QImage file with the size of the aligned scene's rectangle. As the image is created with uninitialized data, you need to call fill() with Qt::transparent to receive a transparent background. You can assign any color you like as an argument both as a value ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.