解决PyQt5的tableWidget数据行不能完全删除问题

时间:2026-02-15 05:32:08

1、在写等额本息程序中用到tableWidget控件,用来存放数据

清理数据行采用

rowPosition = self.tablewidget.rowCount()for rP in range(0, rowPosition):    self.tablewidget.removeRow(rP)

实现,但是问题出现了

解决PyQt5的tableWidget数据行不能完全删除问题

2、for rP in range(0, rowPosition):   self.tablewidget.removeRow(rP)

循环删除,tablewidget 理论上看没问题

但实际上是能清除掉一部分,总是有一部分不能正确清除

解决PyQt5的tableWidget数据行不能完全删除问题

3、经过仔细研究测试。解决方案如下:

采用逆序循环,不能用正序循环!

#这句是关键! range(0, rowPosition)[::-1] 逆序循环

for rP in range(0, rowPosition)[::-1]:      

     。。。

解决PyQt5的tableWidget数据行不能完全删除问题

4、小结:

tablewidget控件循环删除已有行,要用逆序range(0, rowPosition)[::-1]

#先获得总行数 rowPosition 

rowPosition = self.tablewidget.rowCount()

#这句是关键! range(0, rowPosition)[::-1] 逆序循环

for rP in range(0, rowPosition)[::-1]:      

     self.tablewidget.removeRow(rP)

OK了

解决PyQt5的tableWidget数据行不能完全删除问题

© 2026 途途旅游
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com