(pyqt) Parameters when connecting a signal to a method?

exhuma.twn exhuma at gmail.com
Mon Sep 24 11:29:13 EDT 2007


On Sep 24, 4:47 pm, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> exhuma.twn wrote:
> > Some code:
>
> > ----------------------------------------------------------------------
>
> > def foobar(w):
> >    print w
>
> > QtCore.QObject,connect( my_line_edit,
> > QtCore.SIGNAL("returnPressed()"), foobar )
>
> > ----------------------------------------------------------------------
>
> > How can I get this to work so "foobar" prints out the sender of the
> > signal (i.e. my_line_edit)?
>
> I _think_ there is a way to get the signals sender in Qt itself. But I'm
> unsure how to get that.
>
> Alternatively, you can use a closure to create a reference:
>
> def foobar(source, w):
>    print w
>
> def slotgen(source, slot):
>     def _slot(*args):
>         return slot(*((source,) + args))
>     return _slot
>
> my_slot = slotgen(my_line_edit, foobar)
>
> QtCore.QObject,connect( my_line_edit,
> QtCore.SIGNAL("returnPressed()"), my_slot )
>
> However, be careful to keep a reference to my_slot around! Otherwise, it
> will be garbage collected!
>
> diez

Thanks diez. This works :)
Although, I still have to digest *what* this closure does, but I will
leave this as an exercise to myself. ;)




More information about the Python-list mailing list