本文介紹了PyQt5 - 如何從工作線程發出信號以通過 GUI 線程調用事件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
正如我在標題中提到的.我該怎么做?
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
這篇關于PyQt5 - 如何從工作線程發出信號以通過 GUI 線程調用事件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!