我试过了,用了构造函数QImage(const uchar* data, int width, int height, FORMAT format),还有QImage(int width, int height, FORMAT format)和函数fromData(const uchar* data, int len),好像会直接崩溃掉,我的图片格式是RGB888的,内存的长度是768*576*3,不知道是不是QImage的读取超届了。。。
我觉得你能明确那个format没错就成功了一半了吧,对应的QImage的format应该是QImage::Format_RGB888,你可以构造的时候就传入data,或者构造完再修改也可以 另外如果你传入char*作为data的话,要确保这个char*保持有效才可以,Qt是不会自己进行拷贝的,文档说的很清楚: QImage::QImage(uchar * data, int width, int height, Format format, QImageCleanupFunction cleanupFunction = 0, void * cleanupInfo = 0) Constructs an image with the given width, height and format, that uses an existing memory buffer, data. The width and height must be specified in pixels, data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.
The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointer cleanupFunction along with an extra pointer cleanupInfo that will be called when the last copy is destroyed.
If format is an indexed color format, the image color table is initially empty and must be sufficiently expanded with setColorCount() or setColorTable() before the image is used. 似乎从文档看传入的data必须是32位对齐的?即使是RGB888的24位数据?可能下面那个带bytesPerLine的更有用?