简体   繁体   中英

How to clear table widget pyqt5 in order to add new content

I've been trying to figure out how to clear tableWidget so i can add new content. The snippet below shows the widget and how the information is added. However, anytime i refresh it, instead of clearing the screen it rather add more information. Any help will be appreciated.

Thanks

   def addNewContent(results):
        header = self.tableWidget.horizontalHeader()
        self.tableWidget.clearContents()

        header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
        numrows = len(results)
        numcols = len(results[0])

        self.tableWidget.setRowCount(numrows)
        self.tableWidget.setColumnCount(numcols)

        for row in range(numrows):
            for column in range(numcols):
                self.tableWidget.setItem(row, column, QTableWidgetItem((str(results[row][column]))))

    addNewContent(results)

我为这个问题搜索了很多,我想出的唯一解决方案是调用model.clear()然后再次设置标题标签

我认为问题出在变量结果中,我相信它是一个列表,因此您要做的就是重新初始化它,然后在代码中的某处附加新值

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM