[PyKDE] Sending an argument to a function with a button click

Wido Depping wido.depping at tu-clausthal.de
Tue Aug 5 01:09:01 BST 2003


On Monday 04 August 2003 23:40, Simon Edwards wrote:
> On Monday 04 August 2003 23:30, Peter Clark wrote:
> >     I've started dabbling with PyQt, but I'm a little confused as to how I
> > can pass an argument to a function with a button click. For instance, the
> > following doesn't work:
> > (snip button code)
> >
> > self.connect(self.button1, SIGNAL("clicked()"),
>
> self.printMessage("Testing"))
>
> > def printMessage(text):
> >   print text
>
> You can't. In this case printMessage()'s arguments must match clicked(),
> which in PyQt would be:
>
> def printMessage(self): # don't forget the magic self.
>   print "foo"
>
> Why would you want to do this anyway? (You can use different slots for
> different buttons...)

There are some situations, where you have many buttons (perhaps dynamicly 
created), where you want to know who is the sender. It would be ugly, if you 
have to create a slot for every button during runtime.
Instead you can install an eventfilter for every button. This filter gets the 
reference of the sender. This you way can get information which button was 
clicked. Especially if have chosen a unique name for every button.
If someone is interested I can post some code.

bye 
  Wido





More information about the PyQt mailing list