[PyKDE] PyQt4 signals and closures

Phil Thompson phil at riverbankcomputing.co.uk
Sun Jun 25 21:42:35 BST 2006


On Sunday 25 June 2006 6:49 pm, Carlos Scheidegger wrote:
> Hi everyone,
>
> I'm experimenting with PyQt4 signals. According to the documentation,
> any Python callable can be used as a slot. However, I can't get
> closures to work as I would expect them to. Here's some sample code
> that exercises the situation:
>
> from PyQt4 import QtCore
>
> #!/usr/bin/env python
>
> class A(QtCore.QObject):
>      def __init__(self):
>          QtCore.QObject.__init__(self)
>      def makeClosure(self, x):
>          def f():
>              print x
>          return f
>
> a = A()
> a.connect(a, QtCore.SIGNAL("testClosure"), a.makeClosure(5))
>
> a.makeClosure(6)()
>
> a.emit(QtCore.SIGNAL("testClosure"))
>
> Executing this only prints "6" to stdout on my Intel MacBook running
> PyQt4.
>
> PyQt4 version: mac-gpl-snapshot-20060604
> sip version: sip-snapshot-20060525
> Qt version: 4.1.3
>
> Is this a known issue? If any more info is needed to reproduce this,
> let me know.

connect() does not take a reference to a slot, so your closure is getting 
garbage collected immediately.

Phil




More information about the PyQt mailing list