Python 排序后QTableWidget的填充不完整

Python 排序后QTableWidget的填充不完整,python,sorting,pyqt,qtablewidget,Python,Sorting,Pyqt,Qtablewidget,我有一个QTableWidget,它将填充一些随机值。 该表已启用排序:tableWidget.setSortingEnabled(True)。 排序工作很好(我知道,在这个最小的例子中,它将是字母数字排序的数字) 但是,当我按一列对表进行排序,然后在此处使用各种建议清除表时,tableWidget.clear()、tableWidget.clearContent()或tableWidget.setRowCount(0)并重新填充表,表将被不完全填充。我已经注意到,在排序的列之前,表将不完全填充

我有一个QTableWidget,它将填充一些随机值。 该表已启用排序:
tableWidget.setSortingEnabled(True)
。 排序工作很好(我知道,在这个最小的例子中,它将是字母数字排序的数字)

但是,当我按一列对表进行排序,然后在此处使用各种建议清除表时,
tableWidget.clear()
tableWidget.clearContent()
tableWidget.setRowCount(0)
并重新填充表,表将被不完全填充。我已经注意到,在排序的列之前,表将不完全填充。因此,对最后一列进行排序将产生一个完全重新填充的表。但这不是一个可接受的解决办法

但我在这里错过了什么?如何始终完全重新填充表

代码:

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QAction, QTableWidget, QTableWidgetItem, QVBoxLayout, QPushButton
from PyQt5.QtCore import pyqtSlot
import random

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 table'
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(0,0,700,500)
        self.layout = QVBoxLayout()

        self.tableWidget = QTableWidget()
        self.tableWidget.setSortingEnabled(True)
        self.layout.addWidget(self.tableWidget)

        self.pb_refill = QPushButton("Refill")
        self.pb_refill.clicked.connect(self.on_click_pb_refill)
        self.layout.addWidget(self.pb_refill)

        self.setLayout(self.layout) 
        self.show()

    @pyqtSlot()
    def on_click_pb_refill(self):
        # self.tableWidget.clear()
        # self.tableWidget.clearContents()
        self.tableWidget.setRowCount(0)
        rows_random = int(random.random()*7)+5
        self.tableWidget.setRowCount(rows_random)
        self.tableWidget.setColumnCount(6)
        for row in range(rows_random):
            for col in range(6):
                number = random.random()
                self.tableWidget.setItem(row, col, QTableWidgetItem(str(number)))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())
结果:(排序和重新填充后)


问题在于,当您添加项目时,您正在重新排序,例如,假设您已正确插入第一行的值,但随后在(1,0)中输入了一个新项目,该项目比项目(0,0)的重新排序(项目(0,j)将是(1,j)),并且在插入时设置了项目(1,1)将替换占用位置(0,1)的项目,因此项目(0,2)将保持为空

解决方案是通过填充表来禁用排序

@pyqtSlot()
def on_click_pb_refill(self):
    self.tableWidget.clear()
    self.tableWidget.setSortingEnabled(False)
    rows_random = int(random.random() * 7) + 5
    self.tableWidget.setRowCount(rows_random)
    self.tableWidget.setColumnCount(6)
    for row in range(rows_random):
        for col in range(6):
            number = random.random()
            self.tableWidget.setItem(row, col, QTableWidgetItem(str(number)))
    self.tableWidget.setSortingEnabled(True)
@pyqtlot()
def on_单击_pb_加注(自):
self.tableWidget.clear()
self.tableWidget.setSortingEnabled(False)
行随机=int(random.random()*7)+5
self.tableWidget.setRowCount(随机行)
self.tableWidget.setColumnCount(6)
对于范围内的行(随机行):
对于范围(6)中的列:
number=random.random()
self.tableWidget.setItem(行、列、QTableWidgetItem(str(number)))

self.tableWidget.setSortingEnabled(True)
非常感谢!我认为排序只在单击列标题时适用,但不知道它也会在填充表的过程中进行排序。