Python QThread不';不能发出完成的信号
我从另一个QThread运行QThread。第二个线程的Python QThread不';不能发出完成的信号,python,multithreading,pyqt,signals-slots,qthread,Python,Multithreading,Pyqt,Signals Slots,Qthread,我从另一个QThread运行QThread。第二个线程的完成信号未发出。为什么? from PyQt4 import QtGui, QtCore import sys, time thd, thd2 = None, None class T(QtCore.QThread): def __init__(self, f): super().__init__() self.f = f def run(self): self.f() d
完成
信号未发出。为什么?
from PyQt4 import QtGui, QtCore
import sys, time
thd, thd2 = None, None
class T(QtCore.QThread):
def __init__(self, f):
super().__init__()
self.f = f
def run(self):
self.f()
def newThread(f, fin):
t = T(f)
t.finished.connect(fin)
t.start()
return t
def threadInThread():
print("Run.")
global thd2
thd2 = newThread(lambda: print("Run2."), lambda: print("Fin2."))
time.sleep(2)
class Form(QtGui.QWidget):
def __init__(self):
super().__init__()
global thd
thd = newThread(threadInThread, lambda: print("Fin."))
app = QtGui.QApplication(sys.argv)
f = Form()
f.show()
app.exec_()
将QThread对象移动到主线程工作(如果我们将
finished
连接到此对象的插槽)
解释可能重复答案的可能重复:第二个
QThread
的finished
信号连接在第一个QThread
内。这需要第一个QThread
有一个运行事件循环来处理该信号,但它没有,因为您已经覆盖了调用QThread.exec\hmm的QThread.run()
的默认实现
class T(QtCore.QThread):
def __init__(self, f, finish=None):
super().__init__()
self.moveToThread(QtCore.QCoreApplication.instance().thread())
self.f = f
if finish:
self.finish = finish
self.finished.connect(self.onfinish)
def onfinish(self):
self.finish()
def run(self):
self.f()