我试图通过将一些阻塞代码移动到单独的 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 时,我发现它按预期工作,但我不明白为什么。