【问题标题】:pyqt code is blocking although moved to a different QThreadpyqt 代码虽然移动到不同的 QThread,但仍处于阻塞状态
【发布时间】:2014-11-17 16:07:43
【问题描述】:

我试图通过将一些阻塞代码移动到单独的 QThread 来使我的 PyQt4 程序更具响应性。由于它不起作用,我创建了这个最小的示例作为演示:

import sys
import time
from PyQt4 import QtCore, QtGui


class Sleeper(QtCore.QObject):

    def __init__(self):
        super(Sleeper, self).__init__()
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.sleep)
        self.timer.start(1000)

    def sleep(self):
        time.sleep(1)


class MyGUI(QtGui.QMainWindow):

    def __init__(self, parent=None):
        super(MyGUI, self).__init__(parent)
        self.button = QtGui.QPushButton("hello", self)
        self.sleeper = Sleeper()
        self.thread = QtCore.QThread()
        self.sleeper.moveToThread(self.thread)
        self.thread.start()


if __name__ == "__main__":
    qapp = QtGui.QApplication(sys.argv)
    my_gui = MyGUI()
    my_gui.show()
    qapp.exec_()

这段代码的问题是 sleep-command 仍然会阻塞用户界面。当我在 Sleeper 类之外创建、连接和运行 QTimer 时,我发现它按预期工作,但我不明白为什么。

【问题讨论】:

    标签: pyqt qthread qtimer


    【解决方案1】:

    connect 在对象位于 GUI 线程中时被调用,因此事件处理程序也将在 GUI 线程中执行。尝试先移动到线程,然后创建连接。

    class Sleeper(QtCore.QObject):
    
        def __init__(self):
            super(Sleeper, self).__init__()
            self.timer = QtCore.QTimer()
    
        def initialize(self): # Creating the connection and starting separately
            self.timer.timeout.connect(self.sleep)
            self.timer.start(1000)
    
        def sleep(self):
            time.sleep(1)
    
    class MyGUI(QtGui.QMainWindow):
    
        def __init__(self, parent=None):
            super(MyGUI, self).__init__(parent)
            self.button = QtGui.QPushButton("hello", self)
            self.sleeper = Sleeper()
            self.thread = QtCore.QThread()
            self.sleeper.moveToThread(self.thread) # Move to thread
            self.sleeper.initialize() # Create connection now
            self.thread.start()
    

    另外,检查一下: Qt connection type between threads: why does this work?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-14
      • 2013-12-03
      • 2016-12-22
      • 1970-01-01
      • 2012-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多