C++ 从 Qt 中的 QByteArray 加载 QPixmap?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6826683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:43:58  来源:igfitidea点击:

Load QPixmap from QByteArray in Qt?

c++qtqpixmapqbytearray

提问by user662285

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

我有一个包含图像内容的字节数组(png/bmp 或其他格式)。

How can I load it into a QPixmap?

如何将其加载到 QPixmap 中?

回答by Raiv

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

这里的格式是字符串文字"PNG"或类似的东西

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

回答by Matyas

You should use the folowing, where your bytes are in the imageData variable in the format specified by the last parameter:

您应该使用以下内容,其中您的字节以最后一个参数指定的格式位于 imageData 变量中:

QPixmap pixmap = QPixmap::fromImage(
    QImage(
        (unsigned char *) imageData, 
        image_width, 
        image_height, 
        QImage::Format_RGB888
    )
);

回答by Aaron Digulla

Use this constructor:

使用这个构造函数:

QImage ( const uchar * data, int width, int height, Format format )

Here is more info. After that, you can use QPixmap.convertFromImage()to create a pixmap.

这里有更多信息。之后,您可以使用它QPixmap.convertFromImage()来创建像素图。