Skip to main content

PySide2 Signal and Slot

Signal and Slot#

μ‹œκ·Έλ„κ³Ό μŠ¬λ‘―μ„ μ‚¬μš©ν•˜λ©΄ 객체간 톡신을 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

  • ν•˜λ‚˜μ˜ μ‹œκ·Έλ„μ„ μ—¬λŸ¬ μŠ¬λ‘―μ— μ—°κ²°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • μ‹œκ·Έλ„μ€ λ‹€λ₯Έ μ‹œκ·Έλ„μ— μ—°κ²°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • μ‹œκ·Έλ„μ€ QObject의 νŒŒμƒ ν΄λž˜μŠ€μ— 클래슀 λ³€μˆ˜μ— ν• λ‹Ήν•΄μ•Ό ν•©λ‹ˆλ‹€.
  • ν•˜λ‚˜μ˜ μŠ¬λ‘―μ„ μ—¬λŸ¬ μ‹œκ·Έλ„μ— μ—°κ²°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • @Slot λ°μ½”λ ˆμ΄ν„°λ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šμ•„λ„ 슬둯으둜 μ“Έ 수 μžˆμŠ΅λ‹ˆλ‹€.
  • μ“°λ ˆλ“œ μ‚¬μ΄μ˜ 톡신을 μœ„ν•΄ μ‹œκ·Έλ„κ³Ό μŠ¬λ‘―μ„ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
info

A μ‹œκ·Έλ„μ— a μŠ¬λ‘―μ„ μ—¬λŸ¬λ²ˆ μ—°κ²°ν•˜λ©΄, μ‹œκ·Έλ„μ΄ λ°œμƒν–ˆμ„ λ•Œ μ—°κ²°λœ 횟수 만큼 μŠ¬λ‘―μ„ ν˜ΈμΆœν•©λ‹ˆλ‹€.

Examples#

import sys
from PySide2.QtWidgets import QMainWindow, QApplication
from PySide2.QtCore import Signal, Slot
from ui_mainwindow import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
"""
Ui 클래슀 상속 및
μ‚¬μš©μž μ •μ˜ μ‹œκ·Έλ„ 생성
"""
# clickedSignal = Signal((int,), (str,))
pushButtonClickedSignal = Signal(int)
pushButton2ClickedSignal = Signal(str)
def __init__(self):
super().__init__()
"""
Ui μ„€μ •
"""
self.setupUi(self)
self.pushButton.setText("λ²„νŠΌ 1")
self.pushButton2.setText("λ²„νŠΌ 2")
"""
λ²„νŠΌ 클릭 μ‹œκ·Έλ„μ— 슬둯 μΆ”κ°€
"""
self.pushButton.clicked.connect(self.pushButtonClicked)
self.pushButton2.clicked.connect(self.pushButton2Clicked)
"""
μ‚¬μš©μž μ •μ˜ μ‹œκ·Έλ„μ— 슬둯 μΆ”κ°€
"""
# self.clickedSignal[int].connect(self.appendTextBrowser)
# self.clickedSignal[str].connect(self.appendTextBrowser)
self.pushButtonClickedSignal.connect(self.appendTextBrowser)
self.pushButton2ClickedSignal.connect(self.appendTextBrowser)
self.count = 0
def pushButtonClicked(self):
"""
Slot λ°μ½”λ ˆμ΄ν„°κ°€ μ—†μœΌλ©΄ λ§€κ°œλ³€μˆ˜κ°€ μ—†λŠ” 슬둯으둜 λ³Ό 수 있음
"""
self.count += 1
"""
pushButtonClickedSignal 좜λ ₯
Signal.emit(μ •μ˜λœ λ³€μˆ˜)
"""
# self.clickedSignal[int].emit(self.count)
self.pushButtonClickedSignal.emit(self.count)
def pushButton2Clicked(self):
self.count = 0
# self.clickedSignal[str].emit("init count")
self.pushButton2ClickedSignal.emit("init count")
@Slot(int)
@Slot(str)
def appendTextBrowser(self, var):
"""
int λ˜λŠ” str을 λ§€κ°œλ³€μˆ˜λ‘œ 받을 수 μžˆλŠ” 슬둯 생성
"""
if isinstance(var, int):
self.textBrowser.append("count : " + str(var))
else:
self.textBrowser.append(var)
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
import sys
from PySide2.QtWidgets import QMainWindow, QApplication
from PySide2.QtCore import Signal, Slot
from ui_mainwindow import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
"""
2 개의 λ§€κ°œλ³€μˆ˜λ₯Ό κ°–λŠ” μ‹œκ·Έλ„ 생성
"""
clickedSignal = Signal((int, int), (str, int))
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.setText("λ²„νŠΌ 1")
self.pushButton2.setText("λ²„νŠΌ 2")
self.pushButton.clicked.connect(self.pushButtonClicked)
self.pushButton2.clicked.connect(self.pushButton2Clicked)
self.clickedSignal[int, int].connect(self.appendTextBrowser)
self.clickedSignal[str, int].connect(self.appendTextBrowser)
self.count1 = 0
self.count2 = 0
def pushButtonClicked(self):
self.count1 += 1
self.count2 += 1
self.clickedSignal[int, int].emit(self.count1, self.count2)
def pushButton2Clicked(self):
self.count1 = 0
self.clickedSignal[str, int].emit("set count1", self.count1)
@Slot(int, int)
@Slot(str, int)
def appendTextBrowser(self, var1, var2):
if isinstance(var1, int):
self.textBrowser.append("count1: {}, count2: {}".format(var1, var2))
else:
self.textBrowser.append("{} to {}".format(var1, var2))
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())

Reference#

Last updated on