[PyQt] QImage constructors

Giovanni Bajo rasky at develer.com
Thu Jul 9 13:47:25 BST 2009


Hi Phil,

QImage has four different constructors that use existing pixel data:

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

The "const uchar *" are semantically different from the "uchar *" ones; 
quoting the Assistant of the const version:

=====================================================================
Unlike the similar QImage constructor that takes a non-const data 
buffer, this version [the const one] will never alter the contents of 
the buffer. For example, calling QImage::bits() will return a deep copy 
of the image, rather than the buffer passed to the constructor. This 
allows for the efficiency of constructing a QImage from raw data, 
without the possibility of the raw data being changed.
=====================================================================

Unfortunately it looks like PyQt does not wrap the non-const version. 
This is specifically useful because I'm trying to construct QImage over 
a shared memory area and I would like to be able to modify it in-place.

Since QSharedMemory.data() returns a sip.voidptr, I suggest declaring 
the missing constructors as taking a "void*" as first argument for now; 
this would be backward compatible and make my own use case work. For 
Python 3.0, you might want to explore the usage of bytes() vs bytearray().

Let me know if it makes sense. Thanks!
-- 
Giovanni Bajo
Develer S.r.l.
http://www.develer.com


More information about the PyQt mailing list