Search code examples
c++vectortype-conversionqpixmapqbytearray

std::vector<unsigned char> to QPixmap


I can send and get QPixmap by QByteArray:

QPixmap pixmap1;
QByteArray rawPixels;
QBuffer buffer(&rawPixels);
buffer.open(QIODevice::WriteOnly);
pixmap1.save(&buffer, "PNG");

QPixmap pixmap2;
pixmap2.loadFromData(rawPixels,"PNG");

but I have to use std::vector<unsigned char> to send my pixmap:

std::vector<unsigned char> vector;
int size = rawPixels.size();
char* temp = (char*) rawPixels.constData();
vector.resize(size);
for(int index = 0; index < size; index++)
{
    vector.push_back(temp[index]);
}

Is there an easy way to get a QPixmap from a std::vector<unsigned char>?


Solution

  • You can use loadFromData with an unsigned char array

    bool QPixmap::loadFromData ( const uchar * data, uint len, const char * format = 0,
                                 Qt::ImageConversionFlags flags = Qt::AutoColor )
    

    Use vector.data() to get the contiguous underlying data.