I currently have a problem with PyQt5 on Python 3.9.7 (macOS): the code below creates a layout containing a variable number of qtwidgets toggles (and it works correctly).
layout_list = []
for gesture in settings['Gestures']:
layout = QHBoxLayout()
gesture_name = QLabel(gesture)
layout.addWidget(gesture_name)
toggle = Toggle(checked_color = "#00AA00")
layout.addWidget(toggle)
self.gesture_toggle_list.append((gesture, toggle))
layout_list.append(layout)
self.gestureSelector = QVBoxLayout()
for layout in layout_list:
self.gestureSelector.addLayout(layout)
Then, I have to bind each toggle to a different function (or to the same function but with different parameters), so I did this:
for gesture, toggle in self.gesture_toggle_list:
// Prints each gesture and toggle id correctly
print(gesture, str(toggle))
toggle.clicked.connect(lambda: self.toggleGesture(gesture))
and then:
def toggleGesture(self, gesture):
// Should print the gesture related to the toggle clicked
print(gesture)
Unfortunately, whenever I click on one of the toggles, the gesture printed is not the one related to the toggle, but is always the last one on the list (the last gesture seems to override all the others).
Is there a way to fix this problem?