Re: Problem sometimes connecting signal/slot in a loop

Sebastian Schleehauf <[email protected]>
Newsgroups gmane.comp.python.pyqt-pykde
Message-ID <[email protected]>
In the past I have used the following approach which does the trick
quite well by using a dict containing instances and colors instead of a
list with colors. With self.sender() one can obtain the corresponding
color. A functional example can be found below. 
Since the question has been solved allready, I am sending this to find
out if my approach can be considerd "good" and if it has any side
effect I am currently not aware of....
import sys

from PyQt5.QtCore import pyqtSlot
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import QWidget, QPushButton, QColorDialog,
QApplication, QVBoxLayout


class ButtonColorWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.layout = QVBoxLayout()
        self.setLayout(self.layout)
        # Create  some sample buttons with color mapping
        self.button_color_dict = dict()
        for n in range(4):
            button = QPushButton('Button ' + str(n))
            self.button_color_dict[button] = QColor('black')  #
initial_color
            self.layout.addWidget(button)
            button.clicked.connect(self.start_dialog)
        # ColorDialog
        self.color_dialog = QColorDialog(self)

    @pyqtSlot()
    def start_dialog(self):
        button = self.sender()
        if button is None:
            print('Directly calling this method is ignored...')
            return
        new_color =
self.color_dialog.getColor(self.button_color_dict[button])

        if new_color.isValid():
            button.setStyleSheet('QPushButton{color: ' +
new_color.name()+'};')
            self.button_color_dict[button] = new_color
            print('Set new color ', new_color.name())
        else:
            print('Aborted...')
        return


application = QApplication(sys.argv)
widget = ButtonColorWidget()
widget.show()
sys.exit(application.exec_()) 



Am Freitag, den 17.05.2019, 06:34 -0700 schrieb Luna Tuna:
> Thanks, I'll check it out. 
> 
> I just made a local function "connectButton" and that does the
> trick.  But I like the default lambda parameter for more conciseness.
> 
> On Fri, May 17, 2019 at 1:06 AM Florian Bruhin <[email protected]>
> wrote:
> > Hi,
> > 
> > 
> > 
> > another possibility is using functools.partial instead:
> > 
> > 
> > 
> >    
> > button.clicked.connect(functools.partial(self.show_color_dialog,
> > color))
> > 
> > 
> > 
> > IMHO, this is a nice explanation of the issue and possible
> > solutions:
> > 
> > 
https://docs.python-guide.org/writing/gotchas/#late-binding-closures
> > 
> > 
> > 
> > Florian
> > 
> > 
> > 
> > _______________________________________________PyQt mailing
> > list    [email protected]
> > https://www.riverbankcomputing.com/mailman/listinfo/pyqt

_______________________________________________
PyQt mailing list    [email protected]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.