[PyQt] Using functools.partial objects as slots.

Phil Thompson phil at riverbankcomputing.co.uk
Sat Jul 14 15:33:24 BST 2007


On Thursday 12 July 2007 3:34 pm, Phil Thompson wrote:
> 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.

Tonight's SIP snapshot should allow partials to be used as slots without 
needing to keep a reference.

Phil


More information about the PyQt mailing list