信号与槽:
回调函数:
当我们点击按钮时,手指要指定的步骤是:按下按钮 --> 释放按钮
按钮对应会产生三种状态:
对应的代码我们这样来模拟:
import time
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('My Signal&Slot Window 我的信号与槽窗口')
button = QPushButton('My Push Button 我的按钮')
button.setCheckable(True)
button.clicked.connect(self.the_button_was_clicked)
button.pressed.connect(self.the_button_was_pressed)
button.released.connect(self.the_button_was_released)
self.setCentralWidget(button)
def the_button_was_clicked(self):
print(self.windowTitle(), time.time(), time.strftime('%Y-%m-%d %H:%M:%S'), "Button Clicked")
def the_button_was_pressed(self):
print(self.windowTitle(), time.time(), time.strftime('%Y-%m-%d %H:%M:%S'), "Button Pressed")
def the_button_was_released(self):
print(self.windowTitle(), time.time(), time.strftime('%Y-%m-%d %H:%M:%S'), "Button Released")
if __name__ == '__main__':
app = QApplication([])
window = MyWindow()
window.show()
app.exec()
from PySide6.QtCore import QObject, Signal
class Emitter(QObject):
my_signal_0 = Signal()
my_signal_1 = Signal(str) # 创建一个带有1个参数的信号
my_signal_2 = Signal(str, str) # 创建一个带有2个参数的信号
def emit_signal_0(self):
print('emit_signal_0 Send Signal, No Parameters')
self.my_signal_0.emit() # 发射信号
def emit_signal_1(self, a):
print('emit_signal_1 Send Signal, Parameters:', a)
self.my_signal_1.emit(a) # 发射信号并传递1个参数
def emit_signal_2(self, a, b):
print('emit_signal_2 Send Signal, Parameters:', a, b)
self.my_signal_2.emit(a, b) # 发射信号并传递2个参数
class Receiver(QObject):
def slot_function_0(self):
print(self.objectName(), "slot_function_0 Received")
def slot_function_1(self, x): # 槽函数接受1个参数
print(self.objectName(), "slot_function_1 Received:", x)
def slot_function_2(self, x, y): # 槽函数接受2个参数
print(self.objectName(), "slot_function_2 Received:", x, y)
if __name__ == '__main__':
# 发送者
emitter = Emitter()
emitter.setObjectName('TestEmitterObj')
# 接收者
receiver = Receiver()
receiver.setObjectName('TestReceiverObj')
# 连接信号与槽函数
emitter.my_signal_0.connect(receiver.slot_function_0)
emitter.my_signal_1.connect(receiver.slot_function_1)
emitter.my_signal_2.connect(receiver.slot_function_2)
# 发射信号
emitter.emit_signal_0()
emitter.emit_signal_1('one')
emitter.emit_signal_2('one', 'two')
当信号发送时参数个数不匹配时会抛出错误:
emitter.emit_signal_1('one', 'two')
信号的重载在 PySide6 中并不推荐使用,对于从 QT5 遗留的代码可以继续使用这一能力。
示例代码:
import sys
from PySide6.QtCore import QObject, Signal, Slot
from PySide6.QtWidgets import QApplication
class Communicate(QObject):
# create two new signals on the fly: one will handle
# int type, the other will handle strings
speak = Signal((int,), (str,))
def __init__(self, parent = None):
super().__init__(parent)
self.speak[int].connect(self.say_something)
self.speak[str].connect(self.say_something)
# define a new slot that receives a C 'int' or a 'str'
# and has 'say_something' as its name
@Slot(int)
@Slot(str)
def say_something(self, arg):
if isinstance(arg, int):
print("This is a number:", arg)
elif isinstance(arg, str):
print("This is a string:", arg)
if __name__ == "__main__":
app = QApplication(sys.argv)
someone = Communicate()
# emit 'speak' signal with different arguments.
# we have to specify the str as int is the default
someone.speak.emit(10)
someone.speak[str].emit("Hello everybody!")
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。