std :: vector <unsigned char =“”>到QPixmap </unsigned>

时间:2014-10-16 08:49:01

标签: c++ vector type-conversion qpixmap qbytearray

我可以通过QPixmap发送并获取QByteArray

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

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

但我必须使用std::vector<unsigned char>发送我的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]);
}

是否有一种从QPixmap获得std::vector<unsigned char>的简单方法?

1 个答案:

答案 0 :(得分:1)

您可以将loadFromData与unsigned char数组一起使用

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

使用vector.data()获取连续的基础数据。