ste*_*ano 7 python multithreading pyqt qthread
我试图了解如何使用从Qthread返回到启动的Gui接口的信令.
设置:我有一个需要几乎无限期运行的进程(模拟)(或者至少在很长一段时间内运行).当它运行时,它执行各种计算,并且一些结果必须被发送回GUI,将实时适当地显示它们.我正在使用PyQt进行GUI.我最初尝试使用python的线程模块,然后在SO和其他地方阅读了几个帖子后切换到QThreads.
根据Qt博客上的这篇文章你做错了,使用QThread的首选方法是创建一个QObject,然后将其移动到Qthread.所以我在PyQt中跟随了关于QThread的onBackground线程的建议">这个问题并尝试了一个简单的测试应用程序(下面的代码):它打开了一个简单的GUI,让你启动后台进程,并且它发布了更新步骤值一个旋转盒.
但它不起作用.GUI永远不会更新.我究竟做错了什么?
import time, sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class SimulRunner(QObject):
'Object managing the simulation'
stepIncreased = pyqtSignal(int, name = 'stepIncreased')
def __init__(self):
super(SimulRunner, self).__init__()
self._step = 0
self._isRunning = True
self._maxSteps = 20
def longRunning(self):
while self._step < self._maxSteps and self._isRunning == True:
self._step += 1
self.stepIncreased.emit(self._step)
time.sleep(0.1)
def stop(self):
self._isRunning = False
class SimulationUi(QDialog):
'PyQt interface'
def __init__(self):
super(SimulationUi, self).__init__()
self.goButton = QPushButton('Go')
self.stopButton = QPushButton('Stop')
self.currentStep = QSpinBox()
self.layout = QHBoxLayout()
self.layout.addWidget(self.goButton)
self.layout.addWidget(self.stopButton)
self.layout.addWidget(self.currentStep)
self.setLayout(self.layout)
self.simulRunner = SimulRunner()
self.simulThread = QThread()
self.simulRunner.moveToThread(self.simulThread)
self.simulRunner.stepIncreased.connect(self.currentStep.setValue)
self.connect(self.stopButton, SIGNAL('clicked()'), self.simulRunner.stop)
self.connect(self.goButton, SIGNAL('clicked()'), self.simulThread.start)
self.connect(self.simulRunner,SIGNAL('stepIncreased'), self.currentStep.setValue)
if __name__ == '__main__':
app = QApplication(sys.argv)
simul = SimulationUi()
simul.show()
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)
这里的问题很简单:你SimulRunner永远不会收到一个信号,导致它开始工作.这样做的一种方法是将它连接到startedThread 的信号.
另外,在python中你应该使用新式的连接信号的方式:
...
self.simulRunner = SimulRunner()
self.simulThread = QThread()
self.simulRunner.moveToThread(self.simulThread)
self.simulRunner.stepIncreased.connect(self.currentStep.setValue)
self.stopButton.clicked.connect(self.simulRunner.stop)
self.goButton.clicked.connect(self.simulThread.start)
# start the execution loop with the thread:
self.simulThread.started.connect(self.simulRunner.longRunning)
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5943 次 |
| 最近记录: |