将项目添加到QListWidget,而不会冻结UI?

时间:2018-06-19 12:58:59

标签: python pyqt pyqt4

在将大量项目添加到QListWidget而不使UI暂时冻结的过程中,我遇到了一些麻烦。这是我到目前为止的内容:

import sys
from PyQt4 import QtGui, QtCore


class Worker(QtCore.QThread):
    def __init__(self):
        super(Worker, self).__init__()

    def run(self):
        for i in range(25000):
            self.emit(QtCore.SIGNAL('ping(QString)'), str(i))


class ListDemo(QtGui.QListWidget):
    def __init__(self):
        super(ListDemo, self).__init__()

    def addToList(self, item):
        self.insertItem(0, str(item))
        #app.processEvents()


class Demo(QtGui.QDialog):
    def __init__(self):
        super(Demo, self).__init__()
        self.setupUI()

    def setupUI(self):
        self.resize(434, 334)
        self.setWindowTitle('Tester')
        self.mainlayout = QtGui.QVBoxLayout(self)
        self.listwidget = ListDemo()
        self.mainlayout.addWidget(self.listwidget)
        self.button = QtGui.QPushButton('P O P U L A T E    L I S T')
        self.mainlayout.addWidget(self.button)
        self.button.clicked.connect(self.populate)

    def populate(self):
        self.listwidget.clear()
        self.worker = Worker()
        self.connect(self.worker, QtCore.SIGNAL("ping(QString)"), self.listwidget.addToList)
        self.worker.start()


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    myapp = Demo()
    sys.exit(myapp.exec_())

如果启用app.processEvents(),则python崩溃。如果我不进行说明,则UI似乎要等待线程完成才能更新。

我想让UI在每次添加项目时进行更新。有什么办法可以做到这一点?

1 个答案:

答案 0 :(得分:0)

运行速度较慢,但​​UI会定期刷新,并且似乎没有挂起。可能有更好的解决方案,但就目前而言,这似乎可以解决问题。

import sys
import time
from PyQt4 import QtGui, QtCore

class Worker(QtCore.QThread):
    def __init__(self):
        super(Worker, self).__init__()

    def run(self):
        batch = []
        for i in range(25000):
            batch.append(str(i))
            if len(batch) >= 100: # update UI in batches
                time.sleep(0.005) # small time to rest
                self.emit(QtCore.SIGNAL('ping(PyQt_PyObject)'), batch)
                batch = []

        # leftovers
        self.emit(QtCore.SIGNAL('ping(PyQt_PyObject)'), batch)


class MyList(QtGui.QListWidget):
    def __init__(self):
        super(MyList, self).__init__()
        self.setUniformItemSizes(True) #reduces overhead in main thread

    def addToList(self, batch):
        self.insertItems(0, batch[::-1])


class MyWindow(QtGui.QDialog):
    def __init__(self):
        super(MyWindow, self).__init__()
        self.setupUI()

    def setupUI(self):
        self.resize(434, 334)
        self.setWindowTitle('Tester')
        self.mainlayout = QtGui.QVBoxLayout(self)
        self.listwidget = MyList()
        self.mainlayout.addWidget(self.listwidget)
        self.button = QtGui.QPushButton('P O P U L A T E    L I S T')
        self.mainlayout.addWidget(self.button)
        self.button.clicked.connect(self.populate)

    def populate(self):
        self.listwidget.clear()
        self.worker = Worker()
        self.connect(self.worker, QtCore.SIGNAL("ping(PyQt_PyObject)"), self.listwidget.addToList)
        self.worker.start()


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    myapp = MyWindow()
    sys.exit(myapp.exec_())