通过循环不断更新 QTableView

constantly update QTableView via loop

我一直在通过任务创建一个应用程序。该应用程序不断向我抛出数据并将其显示给 table,但是,由于信息太多,该应用程序有时会挂起。我在网上搜索时发现了这个问题:

Update QTableWidget rows with each iteration of loop

实际上我使用的是 QTableWidget 我只是使用了 QTableView 但它没有用。我想要的 table 效果类似于此脚本

import time

for i in range(100000):
    print(i)
    time.sleep(0.3)

我留下我的PyQT5代码:

table.py

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(492, 403)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setObjectName("label")
        self.horizontalLayout.addWidget(self.label)
        self.txt_quantity = QtWidgets.QLineEdit(self.centralwidget)
        self.txt_quantity.setObjectName("txt_quantity")
        self.horizontalLayout.addWidget(self.txt_quantity)
        self.btn_run = QtWidgets.QPushButton(self.centralwidget)
        self.btn_run.setObjectName("btn_run")
        self.horizontalLayout.addWidget(self.btn_run)
        self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 1)
        self.table = QtWidgets.QTableView(self.centralwidget)
        self.table.setObjectName("table")
        self.gridLayout.addWidget(self.table, 1, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 492, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "quantity: "))
        self.btn_run.setText(_translate("MainWindow", "run"))

main.py

import sys
import time
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from table import Ui_MainWindow

class main(QMainWindow):
    def __init__(self):
        super(main, self).__init__()
        self.ui = Ui_MainWindow()        
        self.ui.setupUi(self)        
        self.table()
        self.ui.txt_quantity.setText("100000")
        self.ui.btn_run.clicked.connect(self.run)


    def run(self):       

        for i in range(int(self.ui.txt_quantity.text())):           
            item1 = QStandardItem("item" + str(i))
            item1.setTextAlignment(Qt.AlignCenter)
            item2 = QStandardItem("item" + str(i))
            item2.setTextAlignment(Qt.AlignCenter)
            item3 = QStandardItem("item" + str(i))
            item3.setTextAlignment(Qt.AlignCenter)
            item4 = QStandardItem("item" + str(i))
            item4.setTextAlignment(Qt.AlignCenter)
            item5 = QStandardItem("item" + str(i))
            item5.setTextAlignment(Qt.AlignCenter)
            self.model.appendRow([item1, item2, item3, item4, item5])
            self.ui.table.setModel(self.model)
            # optional the effect I want
            time.sleep(0.3)       



    def table(self):
        self.model = QStandardItemModel()
        self.model.setColumnCount(5)
        self.model.setRowCount(0)
        self.model.setHorizontalHeaderLabels(['header1', 'header2', 'header3', 'header4', 'header5'])
        self.ui.table.setModel(self.model)
        self.ui.table.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch)
        self.ui.table.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch)
        self.ui.table.horizontalHeader().setSectionResizeMode(2, QHeaderView.Stretch)
        self.ui.table.horizontalHeader().setSectionResizeMode(3, QHeaderView.Stretch)
        self.ui.table.horizontalHeader().setSectionResizeMode(4, QHeaderView.Stretch) 



if __name__ == '__main__':
    app = QApplication([])
    application = main()
    application.show()
    sys.exit(app.exec())

非常感谢社区 :)

你不应该使用 time.sleep() 因为它会阻塞事件循环,在这种情况下你可以用迭代器 + QTimer 替换 for-loop + time.sleep()。

class main(QMainWindow):
    def __init__(self):
        super(main, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.table()
        self.ui.txt_quantity.setText("100000")
        self.ui.btn_run.clicked.connect(self.run)
        self.iter = None
        self.timer = QTimer(interval=300, timeout=self.add_row)

    def run(self):
        try:
            rows = int(self.ui.txt_quantity.text())
        except ValueError:
            print("The value entered must be a integer")
        else:
            if rows > 0:
                self.iter = iter(range(rows))
                self.add_row()
                self.timer.start()
            else:
                print("The value entered must be a positive integer")

    def add_row(self):
        try:
            i = next(self.iter)
        except StopIteration:
            self.iter = None
            self.timer.stop()
        else:
            item1 = QStandardItem("item" + str(i))
            item1.setTextAlignment(Qt.AlignCenter)
            item2 = QStandardItem("item" + str(i))
            item2.setTextAlignment(Qt.AlignCenter)
            item3 = QStandardItem("item" + str(i))
            item3.setTextAlignment(Qt.AlignCenter)
            item4 = QStandardItem("item" + str(i))
            item4.setTextAlignment(Qt.AlignCenter)
            item5 = QStandardItem("item" + str(i))
            item5.setTextAlignment(Qt.AlignCenter)
            self.model.appendRow([item1, item2, item3, item4, item5])

    def table(self):
        # ...