自定义信号:
有时系统是没有给我们提供一些信号的,这时就需要我们自定义信号。
例如鼠标单击往往指的是左击,而不是右击,如果想要拥有右击信号,就需要自己自定义信号了!
信号的定义:
槽函数的定义:
信号与槽的连接:
信号的发射:
from PyQt5.Qt import * #刚开始学习可以这样一下导入 import sys class Btn(QPushButton): right_clicked = pyqtSignal([str],[int],[int,int,str]) #中括号的意思是重载,有可能发射 str 也有可能int def mousePressEvent(self,event): super().mousePressEvent(event) if event.button() == Qt.RightButton: #这里解决了什么时候发射信号 # print("应该发射右击信号") self.right_clicked.emit(self.text()) #这里解决了如何将自定义信号发射出去 self.right_clicked[int,int,str].emit(1,1,self.text()) #这里解决了如何将自定义信号发射出去 self.right_clicked[int].emit(1) #这里解决了如何将自定义信号发射出去 class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("信号的学习") self.resize(400,400) self.set_ui() def set_ui(self): btn = Btn("按钮",self) btn.right_clicked.connect(lambda arg:print("右键被点击了",arg)) # btn.right_clicked[int,int,str].connect(lambda v1,v2,arg :print("右键被点击了",v1,v2,arg)) # btn.right_clicked[int].connect(lambda val:print(val)) if __name__ == '__main__': app =QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
装饰器自动连接信号和槽:
from PyQt5.Qt import * #刚开始学习可以这样一下导入 import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("装饰器连接信号和槽的学习") self.resize(400,400) self.set_ui() def set_ui(self): btn = QPushButton("测试按钮",self) btn.resize(200,200) btn.move(100,100) btn.clicked.connect(self.click) def click(self): print("按钮被点击了") if __name__ == '__main__': app =QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
from PyQt5.Qt import * # 刚开始学习可以这样一下导入 import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("装饰器连接信号和槽的学习") self.resize(400, 400) self.set_ui() def set_ui(self): btn = QPushButton("测试按钮", self) btn.resize(200, 200) btn.move(100, 100) ########################################################### btn.setObjectName("btn") ########################################################### # btn.clicked.connect(self.click) ########################################################### QMetaObject.connectSlotsByName(self) # 它一定要等到self 中的对象创建完成 ########################################################### @pyqtSlot() def on_btn_clicked(self): #名字是 on_$ObjectName$_$Slot$ print("按钮被点击了") if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
以后,就算ui 设计中没有定义槽函数也可以,后面我们手工的利用对象的名字来添加信号!
注意:
一个信号连接另外一个信号时,必须保证参数类型和个数一致!
补:自定义信号简单示例(参看文件):
from PyQt5.Qt import * #刚开始学习可以这样一下导入 import sys class Btn(QPushButton): right_clicked = pyqtSignal() def mousePressEvent(self,event): super().mousePressEvent(event) if event.button() == Qt.RightButton: #这里解决了什么时候发射信号 # print("应该发射右击信号") self.right_clicked.emit() #这里解决了如何将自定义信号发射出去 class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("信号的学习") self.resize(400,400) self.set_ui() def set_ui(self): btn = Btn("按钮",self) btn.right_clicked.connect(lambda :print("右键被点击了")) if __name__ == '__main__': app =QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'test.ui' # # Created by: PyQt5 UI code generator 5.13.0 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(400, 300) self.pushButton = Btn(Form) self.pushButton.setGeometry(QtCore.QRect(90, 90, 75, 23)) self.pushButton.setObjectName("pushButton") self.retranslateUi(Form) self.pushButton.right_clicked.connect(Form.right_clicked_slot) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.pushButton.setText(_translate("Form", "PushButton")) from testtest import Btn if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi(Form) Form.show() sys.exit(app.exec_())
from PyQt5.Qt import * #刚开始学习可以这样一下导入 import sys from test import Ui_Form class Window(QWidget,Ui_Form): def __init__(self): super().__init__() self.setWindowTitle("可编辑的文件") self.resize(400,400) self.set_ui() def set_ui(self): self.setupUi(self) def btn_slot(self): print("被点击了我") def right_clicked_slot(self): print("鼠标左键被点击了") if __name__ == '__main__': app =QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())