這個時候就需要我們自定義一個singal,當耗時任務執行完畢後激活singal,通過相應的槽函數來刷新界面或者其他我們想要的操作。
先拿我的gui舉例,我是想同時開始四個線程,然後對應的線程結束後各自更新界面上對應的圖像區域
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import *
import sys
class Deep(QWidget,DP.Ui_Form):
#自定義singal
signal1 = pyqtSignal()
signal2 = pyqtSignal()
signal3 = pyqtSignal()
signal4 = pyqtSignal()
signal5 = pyqtSignal()
signal6 = pyqtSignal()
signal7 = pyqtSignal()
signal8 = pyqtSignal()
signal9 = pyqtSignal()
def __init__(self):
QWidget.__init__(self)
DP.Ui_Form.__init__(self)
self.setupUi(self)
self.evaluation_list=[]
self.state=0
#關聯自定義singal和自定義槽函數
self.signal1.connect(self.signalcall1)
self.signal2.connect(self.signalcall2)
self.signal3.connect(self.signalcall3)
self.signal4.connect(self.signalcall4)
self.signal5.connect(self.signalcall)
self.signal6.connect(self.signalcall)
self.signal7.connect(self.signalcall)
self.signal8.connect(self.signalcall)
self.signal9.connect(self.signalcall5)
self.pushButton_data.clicked.connect(self.showdialog)
self.pushButton_apply.clicked.connect(self.applythread)
def applythread(self):
#四個子線程
ta1= threading.Thread(target=self.startapply1)
ta1.start()
ta2 = threading.Thread(target=self.startapply2)
ta2.start()
ta3 = threading.Thread(target=self.startapply3)
ta3.start()
ta4 = threading.Thread(target=self.startapply4)
ta4.start()
def startapply1(self):
#略去若幹耗時任務代碼
self.signal1.emit()
def signalcall1(self):
#界面圖更新