PDA

View Full Version : How to display a QByteArray as an Image ?



probine
3rd April 2006, 00:09
I have a QByteArray and this one holds an image.

How can I display the image from my QByteArray in a QLabel ?

wysota
3rd April 2006, 00:29
QPixmap image;
QByteArray ba;
image.loadFromData(ba);

probine
3rd April 2006, 11:16
This is my code, and note that the "image.png" is an image located in the same folder as the source code:




int main(int argc, char *argv[])
{
QApplication *application = new QApplication(argc, argv);
QWidget *widget = new QWidget();
widget->setFixedSize(500, 500);
widget->show();
QLabel *label = new QLabel(widget);
label->setGeometry(10, 10, 480, 480);
label->show();
QByteArray *temp = new QByteArray();
QFile *file = new QFile("image.png");
*temp = file->readAll();
QPixmap *pix = new QPixmap();
pix->loadFromData(*temp);
label->setPixmap(*pix);
return application->exec();
}


This gives me a message saying:
QPixmap::convertFromImage: Cannot convert a null image

What am I missing ?

probine
3rd April 2006, 11:23
Thanks for the help. I found the error which was that I was not opening the file

wysota
3rd April 2006, 11:31
Why don't you just load the picture directly from a file?


QPixmap pix("image.png");

probine
3rd April 2006, 12:03
I don't load the picture from the file, because the picture is not located in the local hard disk, so I need to pass this picture from one computer to another.

Is there any other solution ?

probine
3rd April 2006, 12:12
how about displaying the image contained in the QByteArray in a QTextBrowser.

Is it possible, how ?

wysota
3rd April 2006, 13:45
how about displaying the image contained in the QByteArray in a QTextBrowser.

Is it possible, how ?

Make a pixmap out of it like shown in previous posts and load it into the text browser from a document that will reference it (using img src="xxx"). Just make sure this pixmap is available to the browser -- read the docs to see how it's done.