I'm using pyqt and am trying to connect a list of Qpushbuttons to their respective functions and parameters, but they all end up connected to the final parameter instead.
I encountered this issue twice and could only find a solution the first time.
I use a list called vessel to hold information about the buttons I want to create, including the function and a list containing the parameters.
In the setButtons function I take the parameter list and loop through it, replacing the list itself with each parameter and passing the updated vessel into the setButton function.
def setButtons(vessel):
#vessel[1] is the parameter list, vessel[2] is the function
objList = vessel[1]
for obj in objList:
vessel[1] = obj
self.setButton(vessel)
def setButton(vessel):
button = QPushButton(name + value)
button.clicked.connect(lambda: vessel[2](vessel[1]))
Whenever I run the program and click a button, the parameter used will always be that of the final button added, so adding buttons for parameters 'a' 'b' and 'c' and then clicking the 'a' button will just open up a 'c' window. I fixed this by adding an intermediate variable:
def setButton(vessel):
button = QPushButton(name + value)
x = vessel[1]
button.clicked.connect(lambda: vessel[2](x))
After doing this the buttons were all connected to the proper parameters, but I don't understand why this helps. When I encountered a similar issue later seeming to occur in the following update function, the same fix did not help.
def update(self)
for sub in self.subList:
#loops through list containing all subwindows
for l in sub.subVessel:
#subVessel is another list for button info. subVessel[0] is the parameter, [1] is the function, [2] is the button
obj = l[0]
func = l[1]
button = l[2]
button.clicked.disconnect()
button.clicked.connect(lambda: func(obj))