PyQt QThread 多线程不起作用

PyQt QThread MultiThreading does not work

我有 2 个 QListWidget 列表,当从 List1 中选择了某些项目时正在填充 List2

问题是,在填充 List2 之前我必须做很多任务,这些任务会冻结我的 UI 大约 5 秒,这太烦人了,我想让它用 QThread 填充 List2 但它不起作用因为在初始化整个 class 之前我遇到了一个恼人的错误

from ui import Ui_Win
from PyQt4 import QtGui, QtCore

class GenericThread(QtCore.QThread):
    def __init__(self, parent=None):
        QtCore.QThread.__init__(self, parent)

    def __del__(self):
        self.quit()
        self.wait()

    def run(self):
        self.emit( QtCore.SIGNAL('itemSelectionChanged()'))
        return

class MainUI(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self)
        self.ui = Ui_Win()
        self.ui.setupUi(self)
        ...

        genericThread = GenericThread(self)
        self.connect(genericThread, QtCore.SIGNAL("itemSelectionChanged()"), self.fill_List2 )
        genericThread.start()

    def fill_List2(self):
        self.ui.List2.clear()
        list1SelectedItem = str(self.ui.List1.currentItem().text()) # ERROR HERE

回溯: # AttributeError: 'NoneType' object has no attribute 'text'

这是准确的,因为 self.ui.List1.currentItem().text()None

为什么在触发 itemSelectionChanged 信号之前调用此函数?

from ui import Ui_Win ## ui.py is a file that has been generated from Qt Designer and it contains main GUI objects like QListWidget
from PyQt4 import QtGui, QtCore

class GenericThread(QtCore.QThread):
    def __init__(self, parent=None, listIndex=0):
        QtCore.QThread.__init__(self, parent)
        self.listIndex = listIndex

    def __del__(self):
        self.quit()
        self.wait()

    def run(self):
        if self.listIndex == 2:
            for addStr in Something:
                #Some long stuff
                self.emit( QtCore.SIGNAL('fillListWithItems(QString, int'), addStr, self.listIndex)

class MainUI(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self)
        self.ui = Ui_Win()
        self.ui.setupUi(self)
        ...

        self.ui.List1.list1SelectedItem.connect(self.fill_List2)

    @QtCore.pyqtSlot(QString, int)
    def self.fillListWithItems(addStr, lstIdx):
        if lstIdx==2:
            self.ui.List2.addItem(addStr)

    def fill_List2(self):
        self.ui.List2.clear()
        list1SelectedItem = str(self.ui.List1.currentItem().text())

        genericThread = GenericThread(self, listIndex=2)
        self.connect(genericThread, QtCore.SIGNAL("fillListWithItems(QString, int)"), self.fillListWithItems )
        genericThread.start()

谢谢@ekhumoro

你的问题是简单地启动线程会触发 itemSelectionChanged() 信号(因为它在 run 函数中),你已经连接到你的 fill_List2() 函数。您需要将 List1 上的 itemSelectionChanged 事件连接到 SLOT,这将触发线程进行大量计算并更新 List2.

我不得不对 ui 是什么、List1List2 是什么以及它们是如何设置做出一些假设,但这是一个有效的假设例子。我用简单的 2 秒延迟替换了您 GenericThread 中的繁重计算。

如果我误解了/做了错误的假设,请更新问题并发表评论

test_slotting.py

from ui import Ui_Win
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import pyqtSlot
from PyQt4.QtGui import *
import time

class GenericThread(QtCore.QThread):
    def __init__(self, parent=None):
        QtCore.QThread.__init__(self, parent)

    def __del__(self):
        self.quit()
        self.wait()

    def run(self):
        #Do all your heavy processing here
        #I'll just wait for 2 seconds
        time.sleep(2)
        self.emit( QtCore.SIGNAL('itemSelectionChanged()'))
        return

class MainUI(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self)
        self.ui = Ui_Win()
        self.ui.setupUi(self)
        self.ui.List1 = QListWidget(self)
        self.ui.List2 = QListWidget(self)

        hbox = QtGui.QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(self.ui.List1)
        hbox.addWidget(self.ui.List2)

        self.ui.centralWidget.setLayout(hbox)

        self.ui.List1.addItems(['alpha','beta','gamma','delta','epsilon'])
        self.ui.List2.addItems(['Item1','Item2'])

        self.ui.List1.itemSelectionChanged.connect(self.start_heavy_processing_thread)

    @pyqtSlot()
    def start_heavy_processing_thread(self):
        genericThread = GenericThread(self)
        self.connect(genericThread, QtCore.SIGNAL("itemSelectionChanged()"), self.fill_List2 )
        genericThread.start()

    def fill_List2(self):
        self.ui.List2.clear()
        list1SelectedItem = str(self.ui.List1.currentItem().text())
        self.ui.List2.addItem(list1SelectedItem)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = MainUI()
    MainWindow.show()
    sys.exit(app.exec_())

ui.py

from PyQt4 import QtCore, QtGui

class Ui_Win(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(416, 292)
        self.centralWidget = QtGui.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        MainWindow.setCentralWidget(self.centralWidget)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_Win()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())