[PyKDE] Binding parameters to slots

Diez B. Roggisch deets at web.de
Thu Dec 16 15:23:53 GMT 2004


Am Donnerstag, 16. Dezember 2004 16:01 schrieb Jeremy Sanders:
> Hi -
>
> I'm trying to connect a signal in my class to a signal, but bind one of
> its parameters to the signalling object:
>
> For instance, I want to do something like
>
> self.connect( button, qt.SIGNAL('clicked()'),
>                lambda button=button: sys.stdout.write(button) )
>
> or
>
> self.connect( button, qt.SIGNAL('clicked()'),
>                lambda button=button, self=self: self.slotTest(button) )
>
> None of the options I've tried seem to work. Is this possible??

As I learned from Phil two days ago, for your case there exists

self.sender()

that you can call to get the source of your signal.

Before I knew that, I suggested this solution:

class Magic(QObject):
     def __init__(self, dialog, key):
        QObject.__init__(self)
        self.dialog = dialog
        self.key = key

    def focusLost(self):
          self.dialog.keyLostFocus(self.key)


magic = Magic(self, key).focusLost
self.connect(key, SIGNAL("focusLost()"), magic)
self.magics.append(magic)

That should allow you to pass arbitrary parameters to to magic, that you then 
in return can invoke your slot with.

Unfortunately, you have to keep a reference to magic-objects as otherwise the 
GC will hit, and the connection is lost. Found that just yesterday - one 
never stops to learn.....

Diez




More information about the PyQt mailing list