Load QPixmap from QByteArray in Qt?

user662285 picture user662285 · Jul 26, 2011 · Viewed 24.7k times · Source

I have a byte array with the contents of an image (in png/bmp or some other format).

How can I load it into a QPixmap?

Answer

Raiv picture Raiv · Jul 26, 2011
bool QPixmap::loadFromData ( const QByteArray & data, const char * format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor )

Format here is string literal like "PNG" or something similar

QPixmap p;
QByteArray pData;
// fill array with image
if(p.loadFromData(pData,"PNG"))
{
   // do something with pixmap
}