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

Larry Wright larry.tami at gte.net
Tue Aug 5 01:27:00 BST 2003


On Monday 04 August 2003 06:08 pm, Wido Depping wrote:
 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

You don't need to do this at all, QT provides functionality for this already 
in QObject. From the docs for QObject::sender():

Returns a pointer to the object that sent the signal, if called in a slot 
activated by a signal; otherwise it returns 0. The pointer is valid only 
during the execution of the slot that calls this function. 
The pointer returned by this function becomes invalid if the sender is 
destroyed, or if the slot is disconnected from the sender's signal. 
Warning: This function violates the object-oriented principle of modularity. 
However, getting access to the sender might be useful when many signals are 
connected to a single slot. The sender is undefined if the slot is called as 
a normal C++ function. 

 _______________________________________________
 PyKDE mailing list    PyKDE at mats.imk.fraunhofer.de
 http://mats.imk.fraunhofer.de/mailman/listinfo/pykde




More information about the PyQt mailing list