问题描述
正如我在标题中提到的.我该怎么做?
as i mentioned in title. how can i do something like this?
class main(qwidget): def __init__(self): super().__init__() def startbuttonevent(self): self.test = executethread() self.test.start() def myevent(self): #mainthreadgui class executethread(qthread): def run(self): # a lot of work # signal to main thread about finishing of job = mainthread will perform myevent
我在这里找到了一些教程 pyqt4 在线程到主线程中的插槽
i found some tutorials here pyqt4 emiting signals in threads to slots in main thread
这里 从 pyqt qthread 发出信号
但它似乎在 pyqt5 中不起作用:/
but it seems it does not working in pyqt5 :/
推荐答案
只需使用 qthread.finished 信号在这里.如果您完成线程,它将自动执行.当然你也可以根据需要定义自己的自定义信号.
just use the qthread.finished signal here. it will be executed automatically if you finish your thread. of course you can also define your own custom signal if you want.
from pyqt5.qtcore import pyqtsignal class main(qwidget): def __init__(self): super().__init__() def startbuttonevent(self): self.test = executethread() self.test.start() self.test.finished.connect(thread_finished) self.test.my_signal.connect(my_event) def thread_finished(self): # gets executed if thread finished pass def my_event(self): # gets executed on my_signal pass class executethread(qthread): my_signal = pyqtsignal() def run(self): # do something here self.my_signal.emit() pass