德州网站制作,贵州做农业网站,安全之要,做企业销售分析的网站信号重载 在上一篇python之pyqt专栏7-信号与槽3-CSDN博客#xff0c;我们知道在自定义信号时#xff0c;可以设定信号参数数据类型。pyqt还支持信号重载。 信号定义
sendText pyqtSignal([int],[str]) 代码意思是定义重载信号sendText#xff0c;槽函数的参数可以是int数…信号重载 在上一篇python之pyqt专栏7-信号与槽3-CSDN博客我们知道在自定义信号时可以设定信号参数数据类型。pyqt还支持信号重载。 信号定义
sendText pyqtSignal([int],[str]) 代码意思是定义重载信号sendText槽函数的参数可以是int数据类型str类型的参数 注当参数的 Python 类型没有对应的 C 类型时会出错应谨慎使用重载信号。下面是重载信号为字典与列表会出错。 # This will cause problems because each has the same C signature.valueChanged pyqtSignal([dict], [list]) 定义槽 def deal_signal(self,arg):if isinstance(arg,str):# print(arg)self.label.setText(arg)if isinstance(arg, int):print(fage {arg}) 槽函数中判断信号传过来数据类型是int类型还是str类型int将数据打印str类型将数据设置为abel的文本内容 信号与槽绑定 myw.sendText[str].connect(myw1.deal_signal)myw.sendText[int].connect(myw1.deal_signal) 信号进行了重载需要进行两次数据绑定。 发送信号 self.sendText[str].emit(labelStr)self.sendText[int].emit(19) str数据类型信号获取数据lineEdit的文本内容发送 int数据类型信号写定值“19”发送 实现
# 导入sys模块
import sys
# PyQt6.QtWidgets模块中导入QApplication, QWidget
from PyQt6.QtWidgets import QApplication, QWidget
from PyQt6.QtCore import pyqtSignal,pyqtSlot# untitled模块中导入Ui_Form类
# from untitled import Ui_Form
# from untitled1 import Ui_Form
import untitled
import untitled1class MyMainForm(QWidget, untitled.Ui_Form):sendText pyqtSignal([str],[int],)# sendText pyqtSignal(int)def __init__(self, parentNone):# 调用父类的构造函数super(MyMainForm, self).__init__(parent)# 调用继承Ui_Form过来的setupUi函数self.setupUi(self)self.pushButton.clicked.connect(self.btn_clicked)def btn_clicked(self):# 获取行编辑文本labelStr self.lineEdit.text()self.sendText[str].emit(labelStr)self.sendText[int].emit(19)class MyMainForm1(QWidget, untitled1.Ui_Form):def __init__(self, parentNone):# 调用父类的构造函数super(MyMainForm1, self).__init__(parent)# 调用继承Ui_Form过来的setupUi函数self.setupUi(self)self.move(1200,320)pyqtSlot(int)pyqtSlot(str)def deal_signal(self,arg):if isinstance(arg,str):print(arg)self.label.setText(arg)if isinstance(arg, int):print(fage {arg})# Press the green button in the gutter to run the script.
if __name__ __main__:# 实例化应用app QApplication(sys.argv)# 实例化MyMainFormmyw MyMainForm()myw.show()myw1 MyMainForm1()myw1.show()myw.sendText[str].connect(myw1.deal_signal)myw.sendText[int].connect(myw1.deal_signal)# 启动应用程序的事件循环并等待用户交互直到应用程序关闭。sys.exit(app.exec())