[PyQt] Not getting it re sip.voidptr

Phil Thompson phil at riverbankcomputing.com
Sun Nov 4 18:53:18 GMT 2012


On Sun, 4 Nov 2012 09:34:32 -0800, David Cortesi <davecortesi at gmail.com>
wrote:
>>> When I try assignment,
>>>     vptr[1] = 0x00
>>> I get the message,
>>>     TypeError: 'int' does not have the buffer interface
> 
>> I can't reproduce this - can you send a short, complete test that
>> demonstrates the problem.
> 
> Here you go: Ubuntu 12.10, PyQt4 4.9.3-4
> 
> $ python
> Python 2.7.3 (default, Sep 26 2012, 21:53:58)
> [GCC 4.7.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> from PyQt4 import QtCore, QtGui
>>>> img = QtGui.QImage(100,100,QtGui.QImage.Format_ARGB32)
>>>> img.fill(0xdeadbeef)
>>>> vptr = img.bits()
>>>> vptr.setsize(img.byteCount())
>>>> vptr[0] = 0
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: 'int' does not have the buffer interface
>>>>

Fixed in tonight's SIP snapshot - thanks.

> Particularly helpful for this use-case (QImage) would be the ability
> to set or retrieve 32-bit words via the voidptr. It isn't obvious to
> me how to get and set ARGB words from the image data accessed this
> way. The following seems awkward, and I am dubious it would be any
> faster than just calling img.pixel() --
> 
> def get_pixel_as_word( vptr, pixno ) :
>     offset = pixno<<2
>     return (vptr[offset] << 12) | (vptr[1+offset] << 8) |
> vptr[2+offset] << 4) | vptr[3+offset]
> 
> More pythonic and maybe faster:
> 
> def get_pixel_as_tuple( vptr, pixno ) :
>     offset = pixno<<2
>     return (vptr[offset], vptr[1+offset], vptr[2+offset],
vptr[3+offset])

Maybe QImage should support the (new) buffer protocol reflecting the image
format so that the data could be accessed via memoryview.

Phil


More information about the PyQt mailing list