Better way to load QPixmap data

DEgITx picture DEgITx · Jun 26, 2012 · Viewed 8k times · Source

Better way to do it (without QImage)?:

QImage image(width, height, QImage::Format_RGB888);
memcpy(image.bits(), m_frameRGB->data[0], height * width * 3);
QPixmap pixmap = QPixmap::fromImage(image);

I don't see any reason to use QImage as intermediate buffer, but QPixmap::loadFromData don't load data with this context:

pixmap.loadFromData(m_frameRGB->data[0], height * width * 3); // Need pixmap resize?

Answer

hmuelner picture hmuelner · Jun 26, 2012

The documentation says: "If the format is not specified (which is the default), the loader probes the file for a header to guess the file format". You only provide the pointer to the raw image bytes, but you have to provide a header at the start of the buffer, e.g. for an uncompressed PPM format.

Edit: You could also test the suggestion of Roku to use the QImage constructor which takes the image data as a parameter, but see the remark in the documentation: "The buffer must remain valid throughout the life of the QImage."