QThread
Qtμμ μ€λ λλ₯Ό κ΄λ¦¬νλ κΈ°λ³Έ ν΄λμ€
QThreadλ₯Ό μ¬μ©νμ¬ μλ‘μ΄ μ€λ λλ₯Ό μμ±, μ€ν κ°λ₯
κΈ°λ³Έ κ°λ
QThreadλ λ 립μ μΈ μ€ν νλ¦μ κ°λ λ³λμ μ€λ λλ₯Ό μμ±- μ€λ λκ° μμλλ©΄ λ΄λΆμ μΌλ‘
run()λ©μλ νΈμΆ- μ¬μ©μλ
run()λ©μλλ₯Ό μ§μ νΈμΆνμ§ μκ³ ,start()λ©μλλ₯Ό νΈμΆνμ¬ μ€λ λ μμ
- μ¬μ©μλ
QThreadλ μ체 μ΄λ²€νΈ 루νexec()λ₯Ό 보μ ,Signal-Slotν΅μ , νμ΄λ¨Έ λ± μ΄λ²€νΈ μ²λ¦¬ κ°λ₯- μ€λ λκ° μμ μ μννλ λμ λ©μΈ μ€λ λ (GUI)λ μλ΅ μνλ₯Ό μ μ§ β νλ‘κ·Έλ¨μ΄ λ©μΆλ νμ λ°©μ§
μ£Όμ λ©μλ
ꡬν
β1. run() λ©μλ μ€λ²λΌμ΄λ
import sys
from PySide6.QtCore import QThread, Signal, Slot
from PySide6.QtWidgets import (
QApplication, QMainWindow, QWidget,
QVBoxLayout,
QPushButton, QLabel
)
class WorkerThread(QThread):
result_ready = Signal(str) ## μμ
μ΄ μλ£λμμμ λνλ΄λ μκ·Έλ
def run(self) -> None:
for i in range(5):
self.sleep(1)
self.result_ready.emit(f"Count: {i + 1}")
class MainWindow(QMainWindow):
def __init__(self) -> None :
super().__init__()
self.thread = WorkerThread()
self._setup_ui()
self._setup_connections()
def _setup_ui(self) -> None :
## μ°½ μ€μ
self.setWindowTitle("QThread Example")
container = QWidget()
self.setCentralWidget(container)
layout = QVBoxLayout(self)
container.setLayout(layout)
## UI μ€μ
self.label = QLabel("")
self.start_button = QPushButton("Start", self)
layout.addWidget(self.label)
layout.addWidget(self.start_button)
def _setup_connections(self) -> None :
self.thread.result_ready.connect(self.update_text)
self.start_button.clicked.connect(self.start_thread)
@Slot(str)
def update_text(self, text) -> None :
self.label.setText(text)
def start_thread(self) -> None :
self.thread.start()
if __name__ == '__main__' :
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
QThreadλ₯Ό μμλ°λWorkerThreadν΄λμ€λ₯Ό μμ±, λ°±κ·ΈλΌμ΄λ μμ μ μν- μ€λ λμμ μμ
μ΄ μλ£λ λ λ§λ€
result_readyμκ·Έλμ λ°©μΆ - λ©μΈ μλμ°λ μ΄ μκ·Έλμ μμ , ν μ€νΈ μ λ°μ΄νΈ
μ 체 μ½λ
import sys
from PySide6.QtCore import QThread, Signal, Slot
from PySide6.QtWidgets import (
QApplication, QMainWindow, QWidget,
QVBoxLayout,
QPushButton, QLabel
)
class WorkerThread(QThread):
result_ready = Signal(str) ## μμ
μ΄ μλ£λμμμ λνλ΄λ μκ·Έλ
def run(self) -> None:
for i in range(5):
self.sleep(1)
self.result_ready.emit(f"Count: {i + 1}")
class MainWindow(QMainWindow):
def __init__(self) -> None :
super().__init__()
self.thread = WorkerThread()
self._setup_ui()
self._setup_connections()
def _setup_ui(self) -> None :
## μ°½ μ€μ
self.setWindowTitle("QThread Example")
container = QWidget()
self.setCentralWidget(container)
layout = QVBoxLayout(self)
container.setLayout(layout)
## UI μ€μ
self.label = QLabel("")
self.start_button = QPushButton("Start", self)
layout.addWidget(self.label)
layout.addWidget(self.start_button)
def _setup_connections(self) -> None :
self.thread.result_ready.connect(self.update_text)
self.start_button.clicked.connect(self.start_thread)
@Slot(str)
def update_text(self, text) -> None :
self.label.setText(text)
def start_thread(self) -> None :
self.thread.start()
if __name__ == '__main__' :
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()2. moveToThread() μ¬μ©
Reference
PySide6 Threading κ΄λ ¨ κΈ