【问题标题】:std::vector<unsigned char> to QPixmapstd::vector<unsigned char> 到 QPixmap
【发布时间】:2014-12-11 13:29:21
【问题描述】:

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

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

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

但我必须使用std::vector&lt;unsigned char&gt; 发送我的像素图:

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]);
}

有没有从std::vector&lt;unsigned char&gt; 获取QPixmap 的简单方法?

【问题讨论】:

  • 您可以尝试将 vector.data() 转换为 const uchar*。这不是很好,但它应该可以工作。
  • 在执行此操作时,请为自己节省一些行和一个循环。 std::vector&lt;unsigned char&gt; v(rawPixels.constData(), rawPixels.constData()+rawPixels.size()); 可能更喜欢在手动 for 循环中手动执行此操作,除非您有充分的理由不这样做。

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


【解决方案1】:

您可以将loadFromData 与无符号字符数组一起使用

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

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

【讨论】:

  • 矢量应该是什么样子的? {r,g,b,a,r,g,b,a,r,g,b,a} ?如果我想从头开始创建一个。
猜你喜欢
  • 2012-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-18
  • 1970-01-01
  • 1970-01-01
  • 2021-07-11
  • 2014-02-19
相关资源
最近更新 更多