PySide2 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 Main_window(QMainWindow, Ui_MainWindow):
"""
μ‚¬μš©μž μ •μ˜ μ‹œκ·Έλ„ 생성
"""
# clicked_signal = Signal((int,), (str,))
push_button_clicked_signal = Signal(int)
push_button_2_clicked_signal = Signal(str)
def __init__(self):
super().__init__()
self.setupUi(self)
"""
clicked μ‹œκ·Έλ„μ— 슬둯 μΆ”κ°€
"""
self.pushButton.clicked.connect(self.push_button_clickd)
self.pushButton_2.clicked.connect(self.push_button_2_clickd)
"""
μ‚¬μš©μž μ •μ˜ μ‹œκ·Έλ„μ— 슬둯 μΆ”κ°€
"""
# self.clicked_signal[int].connect(self.print_str)
# self.clicked_signal[str].connect(self.print_str)
self.push_button_clicked_signal.connect(self.print_str)
self.push_button_2_clicked_signal.connect(self.print_str)
self.count = 0
def push_button_clickd(self):
"""
Slot λ°μ½”λ ˆμ΄ν„°κ°€ μ—†μœΌλ©΄ λ§€κ°œλ³€μˆ˜κ°€ μ—†λŠ” 슬둯으둜 λ³Ό 수 있음
"""
self.count += 1
"""
push_button_clicked_signal 좜λ ₯
Signal.emit(μ •μ˜λœ λ³€μˆ˜)
"""
# self.clicked_signal[int].emit(self.count)
self.push_button_clicked_signal.emit(self.count)
def push_button_2_clickd(self):
self.count = 0
# self.clicked_signal[str].emit("init count")
self.push_button_2_clicked_signal.emit("init count")
@Slot(int)
@Slot(str)
def print_str(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)
main_window = Main_window()
main_window.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 Main_window(QMainWindow, Ui_MainWindow):
"""
λ§€κ°œλ³€μˆ˜ μ—¬λŸ¬κ°œ μ„€μ •
"""
clicked_signal = Signal((int, int), (str, int))
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.push_button_clickd)
self.pushButton_2.clicked.connect(self.push_button_2_clickd)
self.clicked_signal[int, int].connect(self.print_str)
self.clicked_signal[str, int].connect(self.print_str)
self.count1 = 0
self.count2 = 0
def push_button_clickd(self):
self.count1 += 1
self.count2 += 1
self.clicked_signal[int, int].emit(self.count1, self.count2)
def push_button_2_clickd(self):
self.count1 = 0
self.clicked_signal[str, int].emit("init count1", self.count1)
@Slot(int, int)
@Slot(str, int)
def print_str(self, var1, var2):
if isinstance(var1, int):
self.textBrowser.append("count : {}, {}".format(var1, var2))
else:
self.textBrowser.append("{} : {}".format(var1, var2))
if __name__ == "__main__":
app = QApplication(sys.argv)
main_window = Main_window()
main_window.show()
sys.exit(app.exec_())

Reference

Last updated on