[PyQt] Using functools.partial objects as slots.

Phil Thompson phil at riverbankcomputing.co.uk
Thu Jul 12 15:34:10 BST 2007


On Tuesday 10 July 2007 11:03 pm, Martin Blais wrote:
> Hi Phil
>
> I have this code, which I hoped would work:
>
>      ...
>             for i in xrange(3):
>                 lev = LevelUI(container)
>
>                 curried = partial(self.on_level_price_valueChanged, lev)
>                 QObject.connect(lev.price, SIGNAL("valueChanged(double)"),
>                                 curried)
>
>     ...
>     def on_level_price_valueChanged(self, level, value):
>           ...
>
>
> Unfortunately, partial does not seem to be recognized by connect(). This is
> a workaround to create a closure with in-place evaluation of 'lev' (note
> the keyword arg l=lev)::
>
>             for i in xrange(3):
>                 lev = LevelUI(container)
>
>                 QObject.connect(lev.price, SIGNAL("valueChanged(double)"),
>                                 lambda x, l=lev:
> self.on_level_price_valueChanged(l, x))
>
> but really, wouldn't it be nicer if partial() was supported?

It is - the problem is that it is being garbage collected before it gets 
called.

At the moment only lambda is treated specially. I either need to make partials 
special as well, or bite the bullet and always increment the reference count 
of the slot.

Phil


More information about the PyQt mailing list