这是我要清除数据的程序,我看到了删除行方法,但是它正在删除索引行。如何清除数据而不在表小部件中分配新项目。
下面是我的代码:
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(3, 6, self)
self.table.setGeometry(10, 70, 500,300)
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 Description,Qty,Rate(Rs:),Subtotal,"",").split(','))
all_data = [("1", "Acne-aid Wash Facial Cleansing", 191.72, 0),
("2", "AMoisturizer", 90, 0),
("3", "Brightening eye cream", 40, 0)]
for r, row_data in enumerate(all_data):
for c, value in zip((0, 1, 3, 4), row_data):
it = QtGui.QTableWidgetItem(str(value))
self.table.setItem(r, c, it)
btn = QtGui.QPushButton(icon=QtGui.QIcon("trash1.png"))
btn.clicked.connect(partial(self.cleardata,r))
self.table.setCellWidget(r, 5, btn)
self.setGeometry(200, 300, 600, 300)
self.show()
def cleardata(self,r):
print r
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
答案 0 :(得分:1)
您只需要遍历该行,获取QTableWidgetItem并设置一个空文本:
def cleardata(self, r):
for c in range(self.table.columnCount()):
it = self.table.item(r, c)
if it is not None:
it.setText("")
w = self.table.cellWidget(r, c)
if isinstance(w, QtGui.QLabel):
w.clear()