[PyKDE] Sending an argument to a function with a button click
Jim Bublitz
jbublitz at nwinternet.com
Tue Aug 5 18:29:07 BST 2003
On Monday August 4 2003 16:25, Larry Wright wrote:
> 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.
> 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.
QObject.sender is probably the simplest. You could also subclass
QPushButton and connect the clicked() signal to an internal (to
the subclass) slot which then emits a signal that passes an ID
for the button (object name, serial number, etc). Even simpler,
you could have the subclassed PB's slot simply call another
method and pass the name.
The signal/slot mechanism is really most useful for asynchronous
stuff and once 'clicked()' is emitted/received, the rest of the
sequence is probably synchronous/sequential anyway and wouldn't
really benefit from another signal/slot connection. This being
in Python, it's easy to make the method called by the slot
arbitrary and programmable as well. You can add that feature to
the subclass as well if it's needed.
The event filter method could probably be done cleanly via
subclassing as well.
Jim
More information about the PyQt
mailing list