[PyQt] Problem sometimes connecting signal/slot in a loop

kristof.mulier at telenet.be kristof.mulier at telenet.be
Fri May 17 09:00:20 BST 2019


Hi Luna Tuna, 

I had the exact same problem two years ago. I put a question on StackOverflow, and got a clear answer. You can check it out here: 

https://stackoverflow.com/questions/37319967/pyqt4-signal-slot-mechanism-doesnt-work-correctly-for-list-of-buttons 

The question is for PyQt4, but also applies to the most recent PyQt5. 

I hope this helps ^_^ 
Kind greetings, 

Kristof Mulier 


Van: "Kálmán Viktor" <viktorvector at gmail.com> 
Cc: "pyqt" <pyqt at riverbankcomputing.com> 
Verzonden: Vrijdag 17 mei 2019 00:51:55 
Onderwerp: Re: [PyQt] Problem sometimes connecting signal/slot in a loop 

It looks like you are running into this issue: [ https://docs.python.org/2/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result | https://docs.python.org/2/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result ] 

Luna Tuna < [ mailto:fruitfulapproach at gmail.com | fruitfulapproach at gmail.com ] > ezt írta (időpont: 2019. máj. 16., Cs, 23:43): 



Hi, 
Codes: 

BQ_BEGIN

def setup(self): 
for color in self._colors: 
attr = color + ' color button' 
attr = attr.replace(' ', '_') 
button = getattr(self, attr) 
self._buttons[color] = button 
button.clicked.connect(lambda: self.show_color_dialog(color)) 



When you do something like this, then all connections are made to the last color in the list, 
so clicking on "pen" color will always change "edit locked label" color (the last in list). 

I've fixed it before by making the connection in a dummy function. But is there a better way to resolve this? 

Thanks. 

-Fruitful Approach 
_______________________________________________ 
PyQt mailing list [ mailto:PyQt at riverbankcomputing.com | PyQt at riverbankcomputing.com ] 
[ https://www.riverbankcomputing.com/mailman/listinfo/pyqt | https://www.riverbankcomputing.com/mailman/listinfo/pyqt ] 

BQ_END


_______________________________________________ 
PyQt mailing list PyQt at riverbankcomputing.com 
https://www.riverbankcomputing.com/mailman/listinfo/pyqt 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190517/d196e53d/attachment-0001.html>


More information about the PyQt mailing list