[PyQt] Confusion about const & non-const QImage constructors from raw data

Phil Thompson phil at riverbankcomputing.com
Fri Feb 19 12:39:03 GMT 2010


On Fri, 19 Feb 2010 04:31:36 -0800, TP <wingusr at gmail.com> wrote:
> In http://www.mail-archive.com/pyqt@riverbankcomputing.com/msg18113.html
> Phil Thompson was asking about the non-const QImage constructor:
> 
>   QImage(uchar *data, int width, int height, int bytesPerLine, Format
>   format);
> 
> and suggested implementing it using "void *" as the first argument.
> However, when I
> look at the online doc for PyQt's Qimage() class at
>
http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qimage.html#QImage-6
> I see these two constructors:
> 
>  QImage.__init__ (self, str, int, int, int, Format)
> 
>  QImage.__init__ (self, sip.voidptr, int, int, int, Format)
> 
>   Unlike the similar QImage constructor that takes a non-const data
>   buffer, this version 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.
> 
> So it appears the sip.voidptr version is the const version?

Actually no - the documentation is sometimes misleading.

> I want to use the non-const version, but don't know how to get a str
> as the first argument? If I have the address of my image raw data, how
> do I convert that to a str that QImage.__init__ (self, str, int, int,
> int, Format) will accept?

If you have the address as a number then just pass it to sip.voidptr().

Phil


More information about the PyQt mailing list