使用Python 2.5和PyQt 4.4.3,我無法在Python中找到任何此特定問題,所以對不起,如果我重複下面的其他Qt引用問題,但我不容易理解那C代碼。PyQt線程和信號 - 如何正確檢索值
我有兩個班,一個GUI and a thread,我試圖get return values從線程。我用the link in here作爲編寫my code的基礎,它工作的很好。爲了概括起來,在這裏說明在代碼中的問題(我不認爲這個代碼將在自身運行):
#!/usr/bin/python2.5
# this is a testing module, and almost everything in here is just there to make the script work
# the relevant issues for the testing are noted in comments
from PyQt4 import QtCore, QtGui
import sys, time
class MainWindow (QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.buttonDaemon = QtGui.QPushButton(self)
self.layout = QtGui.QVBoxLayout(self)
self.layout.addWidget(self.buttonDaemon)
self.setLayout(self.layout)
self.thread = Worker()
self.connect(self.thread, QtCore.SIGNAL('finished()'), self.unfreezeUi)
self.connect(self.thread, QtCore.SIGNAL('terminated()'), self.unfreezeUi)
#self.thread.stop.connect(self.stopped) # part of proposed solution
self.connect(self.thread, QtCore.SIGNAL('stopped(int)'), self.stopped) #adapted from proposed solution
self.connect(self.buttonDaemon, QtCore.SIGNAL('clicked()'), self.pressDaemon)
def unfreezeUi (self):
self.buttonDaemon.setEnabled(True)
# the problem begins below: I'm not using signals, or queue, or whatever, while I believe I should for StopSignal and DaemonRunning
def pressDaemon (self):
self.buttonDaemon.setEnabled(False)
if self.thread.isDaemonRunning():
self.thread.setDaemonStopSignal(True)
self.buttonDaemon.setText('Daemon - run code every %s sec'% 1)
else:
self.thread.startDaemon()
self.buttonDaemon.setText('Stop Daemon')
self.buttonDaemon.setEnabled(True)
# part of proposed solution
def stopped (self, val):
print 'stopped ' + str(val)
class Worker (QtCore.QThread):
daemonIsRunning = False
daemonStopSignal = False
daemonCurrentDelay = 0
def isDaemonRunning (self): return self.daemonIsRunning
def setDaemonStopSignal (self, bool): self.daemonStopSignal = bool
def __init__ (self, parent = None):
QtCore.QThread.__init__(self, parent)
self.exiting = False
self.thread_to_run = None
def __del__ (self):
self.exiting = True
self.thread_to_run = None
self.wait()
def run (self):
if self.thread_to_run != None:
self.thread_to_run(mode='continue')
#stop = QtCore.pyqtSignal(int) # part of proposed solution
def startDaemon (self, mode = 'run'):
if mode == 'run':
self.thread_to_run = self.startDaemon # I'd love to be able to just pass this as an argument on start() below
return self.start() # this will begin the thread
# this is where the thread actually begins
self.daemonIsRunning = True
print 'Daemon started'
self.daemonStopSignal = False
sleepStep = 0.1 # don't know how to interrupt while sleeping - so the less sleepStep, the faster StopSignal will work
# begins the daemon in an "infinite" loop
while self.daemonStopSignal == False and not self.exiting:
print 'Daemon running'
# here, do any kind of daemon service
delay = 0
while self.daemonStopSignal == False and not self.exiting and delay < 1:
print 'Daemon sleeping'
time.sleep(sleepStep) # delay is actually set by while, but this holds for 'sleepStep' seconds
delay += sleepStep
# daemon stopped, reseting everything
self.daemonIsRunning = False
print 'Daemon stopped'
#self.stop.emit(self.daemonIsRunning) # part of proposed solution
self.emit(QtCore.SIGNAL('stopped(int)'), self.daemonIsRunning) # adapted from proposed solution
self.emit(QtCore.SIGNAL('terminated'))
def main (args):
app = QtGui.QApplication(args)
win = MainWindow()
win.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main(sys.argv)
芹苴這是相當大的,我希望這是很清楚的。主要觀點在def pressDaemon
。特別是全部3 self.thread
調用。最後一個,self.thread.startDaemon()
就好了,就像例子一樣。我懷疑這代表了任何問題。
問題是能夠設置守護進程停止信號並檢索值,如果它正在運行。我不確定是否可以在QtCore.QtThread
上設置停止信號,因爲我嘗試了相同的方法,但無效。但我很確定從emit
檢索return
結果是不可能的。
所以,它就是這樣。我正在使用直接調用線程類,並且我幾乎肯定這不是一個好設計,並且在壓力下運行時可能會失敗。我閱讀了關於該隊列的內容,但我不確定這是否是正確的解決方案,或者我應該完全使用Qt,因爲這是Python。只是也許我在做的事情沒有任何問題。
編輯:建議的解決方案之後,我用它來使代碼可以運行。再次感謝Max,爲此!而且我也對這個建議留下了評論,讓它清楚地說明它不利於解決問題。
我真的很感激你所有的時間來審查代碼,甚至寫在它的頂部! :)但我無法使用它。我實際上首先認爲這是解決方案,直到能夠嘗試它! :(據我所知,你只是使用新的樣式(http://tinyurl.com/ycdrypv)添加了一個信號,這對我不起作用。我需要**從MainWindow發送一個信號到Thread **,而不是我已經可以做的另一種方式,但我也不確定我應該試着發送這個信號,或者使用其他技術。再次,感謝您的時間,並且確保我會閱讀所有的內容!:) – cregox 2010-05-18 16:31:28
哦,對不起。我誤解了你。 你見過這個話題嗎? http://stackoverflow.com/questions/638251/how-to-emit-cross-thread-signal-in-qt – 2010-05-19 07:07:56
好吧,以前沒見過,但它對我的幫助仍然不大。它不僅僅是C,而且它只是在一個地方發送信號。它確實給了我一個新的'語法猜測'的想法...我會嘗試它並讓你知道。附:在SO/SE網站中,我只收到我的問題的答案,我的帖子的評論(答案或問題)以及我評論的帖子中的提及。在這種情況下,如果您通過提及我或者像我一樣意外通知我,我只會閱讀您的「答案」。 – cregox 2010-05-19 17:17:32