Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   (pyqt) Parameters when connecting a signal to a method? (http://www.velocityreviews.com/forums/t539623-pyqt-parameters-when-connecting-a-signal-to-a-method.html)

exhuma.twn 09-24-2007 02:35 PM

(pyqt) Parameters when connecting a signal to a method?
 
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)?


Diez B. Roggisch 09-24-2007 02:47 PM

Re: (pyqt) Parameters when connecting a signal to a method?
 
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

exhuma.twn 09-24-2007 03:29 PM

Re: (pyqt) Parameters when connecting a signal to a method?
 
On Sep 24, 4:47 pm, "Diez B. Roggisch" <de...@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. ;)



All times are GMT. The time now is 01:28 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.