[PyQt] Using functools.partial objects as slots.

Martin Blais blais at furius.ca
Tue Jul 10 23:03:27 BST 2007


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?

cheers,


More information about the PyQt mailing list