マルチスレッドの処理完了の検知

2020/12/30

Python 3.8.2,PySide2 5.15.2

import sys,time
from PySide2 import QtCore,QtWidgets

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, *argv, **keywords ):
        super(MainWindow,self).__init__(*argv,**keywords)

        self.label = QtWidgets.QLabel(self)
        self.thread = TestProcess(self)
        self.thread.finished.connect(self.setText)

        self.label.setText('処理中')
        self.thread.start()

    def setText(self):
        self.label.setText('処理完了')

class TestProcess(QtCore.QThread):
    def run(self):
        for i in range(5):
            time.sleep(1)

def main():
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()