The problem is that self.table
always points to the last table widget you created. The delRow
method needs to know which table to delete from, so it needs a reference to that table. I would suggest the following code. Here, your slot takes an argument that points to the table you want to delete from.
def addTab(self):
contents = QtGui.QWidget()
table = QtGui.QTableWidget(5, 2)
delButton = QtGui.QPushButton(u"Del Row")
delButton.clicked.connect(lambda: self.delRow(table))
layout = QtGui.QVBoxLayout()
layout.addWidget(table)
layout.addWidget(delButton)
self.tab_widget.addTab(contents, "New Tab")
contents.setLayout(layout)
def delRow(self, table):
table.setRowCount(0)
A couple of things about this code:
I've used the new style signal/slot method to connect the clicked signal to a slot (it is more pythonic)
Because the signal expects to connect to a slot that takes no arguments, I've wrapped your
delRow(table)
method usinglambda
. If you haven't come acrosslambda
before, it is basically short hand for writing a one line function. You can read up about it in the Python docs.