QGraphicsScene scene; // Add items to the scene ... // Render the scene into a QPixmap QPixmap pixmap(800, 600); scene.render(&pixmap); // Save the pixmap to a file pixmap.save("scene.png", "PNG");
QGraphicsScene scene; // Add items to the scene ... // Render a portion of the scene into a QImage QRectF rect(0, 0, 400, 300); // The rect to render QImage image(rect.width(), rect.height(), QImage::Format_ARGB32); image.fill(Qt::transparent); QPainter painter(&image); scene.render(&painter, rect, QRectF(0, 0, rect.width(), rect.height())); // Display the resulting image in a QLabel QLabel label; label.setPixmap(QPixmap::fromImage(image)); label.show();This example creates a new QGraphicsScene, adds some items to it, and then renders a portion of the scene into a QImage using a QRectF. Finally, the resulting image is displayed in a QLabel widget. Both examples use the Qt Framework package library.