PDA

View Full Version : Saving QImage



thgis
29th October 2010, 13:27
Hi.

I have a problem saving a QImage.
It is generated from a QGraphicsScene via the following code:


QImage image(_scene->sceneRect().size().toSize(), QImage::Format_RGB32);
QPainter painter(&image);
_scene->render(&painter);
image.save(filename);

_scene is a QGraphicsScene*
and filename is a non empty string with the format("filename.png")

image.save(filename) always return false.

Another place in my program I save a QImage generated by setting individual pixels in the QImage and that works fine. (The format here is also .png)

I have tried different formats by changing the filename from filename.png to filename.tiff and so on.

Any ideas?

mattc
29th October 2010, 20:11
Are you sure the scene is not empty? QImage::save() returns false if both width and height == 0

thgis
30th October 2010, 01:19
I will check that tomorrow. Thanks for the suggestion.

thgis
1st November 2010, 07:36
I've checked that the image has the correct size and it all looks right.

Any other suggestions?

mattc
1st November 2010, 08:37
The following works fine for me, I can see no difference with your code:


#include <QApplication>
#include <QImage>
#include <QPainter>
#include <QGraphicsScene>

int main(int argc, char** argv)
{
QApplication app(argc, argv);

QGraphicsScene* scene = new QGraphicsScene();
scene->setSceneRect(0, 0, 2, 2);

QImage img(scene->sceneRect().size().toSize(), QImage::Format_RGB32);

QPainter painter(&img);
scene->render(&painter);

bool b = img.save("test.png");

Q_ASSERT(b);

return 0;
}

thgis
1st November 2010, 09:01
Thank you so much mattc for all your effort...

Unfortunately I had made a major error in the construction of the filename... stupid... After correcting this it seems to work fine....:)

very sorry for wasting everyones time...