how can I clear a pyqt QTableWidget?

依然范特西╮ 提交于 2020-12-25 01:10:10

问题


I want to clear my QTableWidget.

First of all I select a user in a qcombobox after that I click a qpushbutton and I populate it from database records; when I select other user and I click the qpushbutton to add data I try to clear with:

self.tableFriends.clear()

The data disappears but the rows remain.

The code I populate with is:

def getFriends(self):
    id_us = self.cbUser.itemData(self.cbUser.currentIndex()).toPyObject()
    rowIndex = 0
    self.tableFriends.clear()
    for row in self.SELECT_FRIENDS(id_us):
        self.tableFriends.insertRow(rowIndex)
        for column in range(0,3):
            newItem = QtGui.QTableWidgetItem(str(row[column]).decode('utf-8'))
            self.tableFriends.setItem(rowIndex,column,newItem)
        rowIndex = rowIndex + 1

回答1:


You need to remove the rows individually, e.g:

while (self.tableFriends.rowCount() > 0)
{
    self.tableFriends.removeRow(0);
}

You can also try:

tableFriends.setRowCount(0);

But that may not work, it's been a while since I've used Qt.



来源:https://stackoverflow.com/questions/29266452/how-can-i-clear-a-pyqt-qtablewidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!