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

E

exhuma.twn

Some code:

----------------------------------------------------------------------

def foobar(w):
print w

QtCore.QObject,connect( my_line_edit,
QtCore.SIGNAL("returnPressed()"), foobar )
 
D

Diez B. Roggisch

exhuma.twn said:
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
 
E

exhuma.twn

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. ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top