[PyQt] How to handle sip_voidptr

Phil Thompson phil at riverbankcomputing.com
Sat Nov 9 10:08:38 GMT 2013


On Fri, 8 Nov 2013 22:48:01 -0500, Burak Nehbit <burak at nehbit.net> wrote:
> Hi,
> 
> I’m trying to get Qt’s native events support working in PyQt.
> 
> As per this
>
documentation: http://qt-project.org/doc/qt-5.0/qtcore/qabstractnativeeventfilter.html 
> 
> I wrote the following code:
> 
> class MacEventFilter(QtCore.QAbstractNativeEventFilter):
>     def __init__(self):
>         super(MacEventFilter, self).__init__()

You don't need the __init__ method as it's not doing anything that the
default implementation doesn't do.

>     def nativeEventFilter(self, QByteArray, sip_voidptr):

That's not the signature of the method, see...

help(QAbstractNativeEventFilter.nativeEventFilter)

>         sip_voidptr.setsize(4) #32 bit (?)
>         cobject = sip_voidptr.ascobject()
>         print(repr(cobject))
>         #cp = cast(sip_voidptr.asstring(), POINTER(c_void_p))
>         print(cp.contents)
>         return True
> 
> mef = MacEventFilter()
> app.installNativeEventFilter(mef)
> 
> The problem I am having is I do not know know to deal with sip_voidptr.
> The documentation casts this into … something, and continues to use it.
I’m
> working on a Mac application, so in that case it seems I have to cast
this
> into an EventRef. 
> 
> The sole documentation I can find about EventRef is
>
this: http://oreilly.com/catalog/learncarbon/chapter/ch06.html#CHAP-CH06-6
> 
> Is this already a SIP, Qt or PyQt object? How can I cast this? Python
does
> not do casting to classes(structs?), so there must be some way in SIP to
> handle this. But I do not know where to start looking or what is it
> called. 

SIP or PyQt doesn't know anything about EventRef. You should use
struct.unpack() to convert the voidptr to something useful.

The second value your nativeEventFilter() should return can be 0 as it is
only used on Windows.

> After I cast this, I will (hopefully) have an event object that can give
> me information about the event.
> 
> My ultimate purpose is to be aware when my user clicks the dock icon on
> Mac. Clicking on the dock icon should draw the UI if it is closed, and
> bring the UI to the front if it is open but in the background. Qt
> unfortunately does not offer a native way to handle click to the dock
icon.
> 
> Any pointers on where to start?
> 
> Regards,
> Burak

Phil


More information about the PyQt mailing list