import sys
from PySide2.QtWidgets import QMainWindow, QApplication
from PySide2.QtCore import (
QObject,
QThread,
QRunnable,
QThreadPool,
Slot,
Signal,
)
from ui_mainwindow import Ui_MainWindow
class TaskSignal(QObject):
"""
QRunnable에서 사용할 시그널
"""
finished = Signal(bool)
class Task(QRunnable):
_taskSignal = TaskSignal()
finished = _taskSignal.finished
var = False
def run(self):
"""
잠시 중단하는 경우 QThread.msleep() 사용
"""
QThread.msleep(5000)
self.finished.emit(self.var)
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.setText("버튼 1")
self.pushButton.clicked.connect(self.startTask)
self.task = Task()
self.task.setAutoDelete(False)
self.task.finished.connect(self.taskFinished)
def startTask(self):
if self.task.var:
self.task.var = False
else:
self.task.var = True
"""
task 실행
"""
QThreadPool.globalInstance().start(self.task)
"""
버튼 비활성화
"""
self.pushButton.setEnabled(False)
@Slot(bool)
def taskFinished(self, is_success):
if is_success:
self.textBrowser.append("success")
else:
self.textBrowser.append("fail")
self.pushButton.setEnabled(True)
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())