I have many QPushButton
s in a QTableWidget
:
from PyQt5 import QtWidgets
import sys
class Mainwindow(QtWidgets.QWidget):
def __init__(self):
super().__init__()
table = QtWidgets.QTableWidget(3, 1)
l = QtWidgets.QHBoxLayout()
l.addWidget(table)
self.setLayout(l)
for i in range(3):
btn = QtWidgets.QPushButton('button '+str(i))
btn.clicked.connect(lambda: self.btnSlot(i))
layout = QtWidgets.QHBoxLayout()
layout.setContentsMargins(0,0,0,0)
container = QtWidgets.QWidget()
container.setLayout(layout)
layout.addWidget(btn)
table.setCellWidget(i, 0, container)
def btnSlot(self, num):
print(num)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
m = Mainwindow()
m.show()
app.exec()
Why is this snippet printing 2
no matter which button I push?
I would expect button 0
to print 0
, button 1
to print 1
etc, since I connect to a lambda function with the loop counter i
.
What do I have to change to get the behaviour I expect?