Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create QImage from buffer, no deep copy?

Tags:

c++

qt

in Qt, how can I construct a QImage object from a byte array ('uint8_t*`), without creating a deep copy, just referring to my data array?

like image 262
manatttta Avatar asked Mar 12 '23 22:03

manatttta


1 Answers

Use any of the constructors which take a uchar *, for instance

QImage::QImage(uchar *data, 
               int width, 
               int height, 
               int bytesPerLine, 
               Format format, 
               QImageCleanupFunction cleanupFunction = Q_NULLPTR, 
               void *cleanupInfo = Q_NULLPTR)

As the documentation says:

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.

like image 176
peppe Avatar answered Mar 20 '23 20:03

peppe