排序后 QTableWidget 的填充不完整 [英] Incomplete population of QTableWidget after sorting

查看:64
本文介绍了排序后 QTableWidget 的填充不完整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a QTableWidget which will be populated with some random values. The table has sorting enabled: tableWidget.setSortingEnabled(True). Sorting works fine (I know, in this minimal example it will be alphanumerical sort of numbers).

但是,当我按一列对表格进行排序时,然后在 SO 上使用各种建议清除表格,tableWidget.clear()tableWidget.clearContent()tableWidget.setRowCount(0) 并重新填充表格,表格将不完整地填充.我已经注意到,在排序的列之前,表格将不完全填充.所以对最后一列进行排序将导致一个完全重新填充的表.但这不是一种可接受的解决方法.

However, when I've sorted the table by one column, then clearing the table with various suggestions here on SO, either tableWidget.clear(), tableWidget.clearContent() or tableWidget.setRowCount(0) and repopulate the table, the table will be populated incompletely. I already noticed that the table will be imcompletely populated until the column which is sorted. So sorting the last column would result in a completely refilled table. But that's not an acceptable workaround.

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

But what am I missing here? How to always fully re-populate the table?

代码:

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) 将保持为空.

The problem is that while you are adding items you are reordering, for example let's say you have inserted the value of the first row correctly but then you enter a new item in (1, 0) that is less than the item (0, 0) by what will be reordered (items (0, j) will be (1, j)), and when you insert you set item (1, 1) will replace the item that occupied position (0, 1) so item (0 , 2) will remain empty.

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

The solution is to disable sorting by populating the table.

@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)

这篇关于排序后 QTableWidget 的填充不完整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆