[PyQt] multiple signal connect

Daniel Miller daniel at keystonewood.com
Wed Aug 20 20:17:27 BST 2008


Ahh, sorry, that will have the same problem you had before. Here's a  
better version:


def slotmaker(button):
     def slot():
         self.button_clicked(button)
     button.__slot = slot

for button in [ ... ]:
     QtCore.QObject.connect( ... , slotmaker(button))


Note: it might not be possible to keep a reference to the slot on the  
button itself--not sure if attribute assignment is possible directly  
on qt objects that are not subclassed in Python. If not just keep a  
list of slots attached to 'self' (your class) or something like that.

~ Daniel


On Aug 20, 2008, at 3:09 PM, Daniel Miller wrote:

> Try this:
>
> for button in [...]:
>     def slot():
>         self.button_clicked(button)
>     button.__slot = slot # keep a reference so it doesn't get GC'd
>     QtCore.QObject.connect(..., slot)
>
>
> ~ Daniel
>
>
>
>
> On Aug 20, 2008, at 11:17 AM, Ruben Fonseca wrote:
>
>> Hi,
>>
>> I'm having a problem developing my first Python Qt4 app and I thought
>> you could help me.
>>
>> I have a bunch of buttons that I want to watch for the "clicked()"
>> signal.
>>
>> Since the handling is the same for all buttons I decided to write
>> something like this
>>
>> ----------
>> for button in [self.button_1, self.button_2, ..., self.button_0]:
>>    QtCore.QObject.connect(button, QtCore.SIGNAL('clicked()'), lambda:
>> self.button_clicked(button))
>>
>> def button_clicked(self, number):
>>    print "Received a signal from %s" % number.objectName()
>> ----------
>>
>> However, every time I click on a button I got on the console
>>
>> "Received a signal from button_0"
>>
>> regardless the button I click... So I'm guessing something's wrong  
>> with
>> my lambda function.
>>
>> What's the best way of solving this particular problem in Python?
>>
>> Thank you for you support
>> Ruben Fonseca
>> _______________________________________________
>> PyQt mailing list    PyQt at riverbankcomputing.com
>> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>



More information about the PyQt mailing list