PDA

View Full Version : Image export looks messed up



blooglet
24th February 2011, 11:12
I'm trying to export the contents of my QGraphicsScene to PNG. For some reason (http://www.qtcentre.org/threads/4368-QGraphicsScene-evolution-of-sceneRect) I have to maintain the scene's bounding rectangle myself.

Here's a sample export that should contain a purple ellipse and an orange rectangle on a transparent background:

http://i.imgur.com/WHOYL.png (http://imgur.com/WHOYL)

Why is the exported image messed up like that? Are there any common symptoms that I should look for in my code?

wysota
27th February 2011, 10:04
How exactly do you export the scene to the image?

blooglet
27th February 2011, 17:23
QString filename = QFileDialog::getSaveFileName(this, "Export image", "", "Images (*.png)");
int imgWidth = this->myGraphicsScene->width();
int imgHeight = this->myGraphicsScene->height();
QImage qimg(imgWidth, imgHeight, QImage::Format_ARGB32);
QPainter qpainter;
qpainter.begin(&qimg);
this->myGraphicsScene->render(&qpainter);
qpainter.end();
qimg.save(filename);

wysota
27th February 2011, 18:19
You have to fill your image with transparency first. Otherwise you'll have it initialized with whatever happens to be in the chunk of memory assigned as buffer to your image.

qimg.fill(Qt::transparent);

blooglet
27th February 2011, 18:27
Yeeha, that works! Thanks!