How to delete entire row in pyqt4 (qtable widget)

How to delete entire row in pyqt4 (qtable widget)



Here when I am trying to delete the row selected I am getting the following error:



"TypeError: argument 1 of QAbstractItemModel.removeRow() has an invalid type".



I have searched a lot for the correct way of deleting selected row/rows in qtablewidget of pyqt. However, I am not able to delete the selected row/rows.



Can you please share a sample code for deleting selected row/rows in qtablewidget of pyqt?


import sys
from functools import partial
from PyQt4 import QtGui, QtCore


class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()

def initUI(self):
self. table = QtGui.QTableWidget(self)
self.table.setGeometry(10,70, 600,300)
self.table.setRowCount(3)
self.table.setColumnCount(6)

self.table.verticalHeader().hide()
self.table.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)

fnt = self.table.font()
fnt.setPointSize(11)
self.table.setFont(fnt)

self.table.setHorizontalHeaderLabels(("S.no, Item,Qty,Rate(Rs:),total,"",").split(','))

all_data = [("1", "xxx", 10, 0),
("2", "yyy", 20, 0),
("3", "zzz", 30, 0)]

for r, row_data in enumerate(all_data):
for c, value in zip((0, 1, 3), row_data):
it = QtGui.QTableWidgetItem(str(value))
self.table.setItem(r, c, it)

for r in range(self.table.rowCount()):

spin = QtGui.QSpinBox(minimum=0, maximum=50)
spin.valueChanged.connect(partial(self.calculateSubTotal, r))
self.table.setCellWidget(r, 2, spin)

btn = QtGui.QPushButton(icon=QtGui.QIcon("trash1.png"))
self.table.setCellWidget(r, 5, btn)
# selected = self.table.selectedItems()
# btn.clicked.connect(self.remove)

self.setWindowTitle("table")
self.setGeometry(200, 300, 400, 300)
self.show()
# def remove(self,row):
# self.table.removeRow(self.table.currentRow)

def calculateSubTotal(self, row, value):
rate = float(self.table.item(row, 3).text())
subtotal = value * rate

item_subtotal = self.table.item(row, 4)
if item_subtotal is None:
item_subtotal = QtGui.QTableWidgetItem()
self.table.setItem(row, 4, item_subtotal)

item_subtotal.setText(str(subtotal))

def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())


if __name__ == '__main__':
main()




2 Answers
2



When you press the button the QTableWidget is not notified since the events of the widgets are not notified to the QTableWidget so currentRow will not serve you. instead you should use the variable r of the for-loop, and for that we use functools.partial(...)


QTableWidget


QTableWidget


currentRow


r


functools.partial(...)


for r in range(self.table.rowCount()):
...

btn = QtGui.QPushButton(icon=QtGui.QIcon("trash1.png"))
self.table.setCellWidget(r, 5, btn)
btn.clicked.connect(partial(self.table.removeRow, r)) # <---



Your syntax was almost correct, try to add brackets :


self.table.removeRow(self.table.currentRow())



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ