I want to programmaticaly create sliders and connect them to simple slot with the following :
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow, QApplication, QVBoxLayout, QSlider, QWidget
class VuePyQt(QMainWindow):
def __init__(self, parent = None):
super(VuePyQt, self).__init__()
self.params = ["a", "b"]
layout = QVBoxLayout()
for pname in self.params:
# create new slider
slider = QSlider(Qt.Horizontal)
# connect signal to a simply slot that depends on "key"
slider.valueChanged.connect(lambda : print(pname))
# add slider to layout
layout.addWidget(slider)
# add reference
setattr(self, pname+"_slider", slider)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
def main():
app = QApplication(sys.argv)
vue = VuePyQt()
vue.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
This create a window with 2 sliders, one for each parameters, a and b.
What I expect
Moving the first slider prints "a", and moving the second slider prints "b"
What I get
Moving any slider prints "b"
What I tried
Adding more parameters or changing the order shows that it is always the last parameter that was set that is printed for all sliders.