[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: [PyKDE] "sender" info in Python slot
From:       Phil Thompson <phil () riverbankcomputing ! co ! uk>
Date:       2006-11-10 18:31:37
Message-ID: 200611101831.37445.phil () riverbankcomputing ! co ! uk
[Download RAW message or body]

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.
> >
> > An alternative pythonic way to do this is to do something like:
> >
> > class BoundCaller(object):
> >     """A callable class to wrap a function and its arguments."""
> >     def __init__(self, fn, *params):
> >         self.fn = fn
> >         self.params = params
> >     def __call__(self, *params):
> >         self.fn( *(self.params+params) )
> >
> > in your class:
> >
> >      def __init__(self):
> >          self.myslot1 = BoundCaller(myslot, 1)
> >          self.myslot2 = BoundCaller(myslot, 2)
> >
> >          self.connect(self.mybutton, SIGNAL('clicked()'), self.myslot1)
> >          ...
> >
> >      def myslot(self, boundarg):
> >          print boundarg
> >
> > You have to keep a separate reference to the BoundCaller object (hence
> > the assignment to self), otherwise it gets garbage collected.
>
> Hi,
>
> I still think that connect not taking a reference on the slot is a mistake,
> otherwise you could just use a lambda here.

It's something I've thought about changing. The decision not to was taken 
before the existence of the cyclic garbage collector. Something for next year 
maybe.

Phil

_______________________________________________
PyKDE mailing list    PyKDE@mats.imk.fraunhofer.de
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic