[PyKDE] "sender" info in Python slot

Andreas Pakulat apaku at gmx.de
Sat Nov 11 10:10:13 GMT 2006


On 11.11.06 07:11:22, Mark Summerfield wrote:
> On Fri 10-Nov-06 18:31, Phil Thompson wrote:
> > On Thursday 09 November 2006 8:54 pm, Torsten Marek wrote:
> > > Jeremy Sanders schrieb:
> > > > On Thu, 9 Nov 2006, Andreas Pakulat wrote:
> > > >> On 09.11.06 16:43:00, Sibylle Koczian wrote:
> > > >>> hopefully the subject line isn't too misleading. This is my problem:
> > > >>> I would like to connect the "clicked" signal of several buttons to
> > > >>> one and the same function - but this function should know which
> > > >>> button sent the signal. Example: one button to increase a value,
> > > >>> another to decrease it, and one single function which adds '+1' or
> > > >>> '-1' depending on the sending button. Or a row of buttons with a
> > > >>> letter, and clicking one searches for all words beginning with this
> > > >>> letter in a list.
> > > >>>
> > > >>> How can I do this? Subclassing QPushButton seems to be a possibility,
> > > >>> but is there a simpler way?
> > > >>
> > > >> The unclean solution would be to use the sender() function, the proper
> > > >> one is to have a look at QSignalMapper.
> [snip]
> 
> Using sender() is not considered "unclean" in Qt 4, so I would feel free
> to use it.

Where did you get that from? Even the Qt docs state that its unclean:

QObject * QObject::sender () const   [protected]
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.

Its used sometimes, IIRC even inside Qt but it still is unclean and a
solution as QSignalMapper should always be considered better if it can
be applied.

Andreas

-- 
You will be given a post of trust and responsibility.




More information about the PyQt mailing list