![](/img/trans.png)
[英]why signal emitting in QDialog can not invoke the connected slot in QThread?
[英]PySide - QThread running but not entering slot connected to signal 'started'
我以為我已經閱讀了有關Qt中線程的全部知識,但顯然我弄錯了。 我在這方面停留了一段時間,因此,非常感謝您的幫助。
因此,我創建了一個在其__init__方法中啟動線程的類:
class MyClass(object):
def __init__(self):
(...)
self.thread = QtCore.QThread(parent)
worker = Worker()
QtCore.QObject.connect(self.thread, QtCore.SIGNAL('started()'), worker,
QtCore.SLOT('doWork()'))
QtCore.QObject.connect(worker, QtCore.SIGNAL('finished()'), self.thread,
QtCore.SLOT('quit()'))
QtCore.QObject.connect(worker, QtCore.SIGNAL('finished()'), worker,
QtCore.SLOT('deleteLater()'))
QtCore.QObject.connect(self.thread, QtCore.SIGNAL('finished()'),
self.thread, QtCore.SLOT('deleteLater()'))
worker.moveToThread(self.thread)
self.thread.start()
工人階級看起來像這樣:
class Worker(QtCore.QObject):
(some signals)
def doWork(self):
print "doing my work"
問題是盡管線程正在運行,但我的doWork插槽從未執行。
創建MyClass實例后,讓我們說:obj = MyClass()
我可以打電話給:obj.thread.isRunning()
返回True。
我假設未發出“開始”信號,因為會立即退出創建線程的方法(當我在啟動線程后添加睡眠時,它會進入doWork插槽)。 我不知道應該如何正確處理這種情況。
如果我沒有清楚地說明我的問題,請告訴我。 謝謝大家的幫助。
MyClass
構造函數返回后,將對您的工作程序進行垃圾回收。 使worker
成為MyClass
的成員,這樣它就可以在構造函數之外繼續存在。
self.worker = Worker()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.