【问题标题】:how can I clear a pyqt QTableWidget?如何清除 pyqt QTableWidget?
【发布时间】:2015-05-29 18:15:13
【问题描述】:

我想清除我的 QTableWidget。

首先,我在 qcombobox 中选择一个用户,然后单击 qpushbutton 并从数据库记录中填充它;当我选择其他用户并单击 qpushbutton 添加数据时,我尝试清除:

self.tableFriends.clear()

数据消失,但行仍然存在。

我填充的代码是:

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

【问题讨论】:

    标签: python-2.7 pyqt qtablewidget


    【解决方案1】:

    您需要单独删除行,例如:

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

    你也可以试试:

    tableFriends.setRowCount(0);
    

    但这可能行不通,我已经有一段时间没有使用 Qt 了。

    【讨论】:

    • 非常感谢。我添加了 tableFriends.setRowCount(0) 就足够了。
    猜你喜欢
    • 1970-01-01
    • 2017-06-17
    • 2012-05-03
    • 2016-12-29
    • 2014-05-03
    • 2018-08-02
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    相关资源
    最近更新 更多