The proper use of QSignalMapper

borntonetwork borntonetwork at gmail.com
Sun Jan 21 01:47:40 CET 2007


How simple. I will remember that sender() function for future
reference.
Thanks, David.

David Boddie wrote:
> On Saturday 20 January 2007 16:03, borntonetwork wrote:
>
> > David, thanks for your help. Unfortunately, all attempts of making this
> > solution work have failed. I would be interested to know if anyone has
> > used QSignalMapper successfully in a similar situation.
>
> Looking again at what you originally wrote, and looking at your
> solution below, I don't think it's really appropriate to use a
> QSignalMapper for this purpose. (That's what I understood from the
> signature of the deleteProductIngredient() method you had in your
> original code.)
>
> What you appear to want is a way of being notified about state
> changes to widgets that also identifies the individual widget
> that changed. You can do that by connecting the individual widgets
> to the same slot and call sender() to find out which object emitted
> the signal; for example:
>
>         # ...
>
>         for idx in range(1, maxIngredients+1):
>             wName = 'chkProductIngredientsDelete_'+str(idx)
>             w = self.__dict__[wName]
>             self.connect(w, QtCore.SIGNAL("stateChanged(int)"),
>                          self.deleteProductIngredient)
>
>   def deleteProductIngredient(self, state):
>       """Delete a product ingredient."""
>       print self.sender(), "changed state to", state
>
> What QSignalMapper does is collect signals from different objects,
> assigning an ID to each of them, and emits a single signal with
> an ID whenever it receives a signal from one of those objects.
> In other words, it allows the actions of a group of objects to be
> described by a parameter but discards the signal's arguments.
> It's most useful for things like collections of push buttons where
> you only need to know that they were clicked.
>
> > For any interested, I worked around the problem using a closure,
> > which seems a bit cleaner from a coding point of view, although
> > I don't have any idea if it is more efficient or not:
>
> [...]
>
> > Each iteration then created a new function based on the closure:
>
> [...]
>
> It's good to know that you got something working in the end. There's
> also another solution that involves QSignalMapper, but it may not make
> the resulting code any simpler.
> 
> David




More information about the Python-list mailing list