自定义信号和槽的步骤:
1、定义信号---signal1 = pyqtSignal()
2、定义槽信号---def setSlot(self):
3、连接信号和槽函数---signal1.connect(self.setSlot)
4、发射信号---signal1.emit()
案例1:
from PyQt5.QtCore import QObject,pyqtSignal
from PyQt5.QtWidgets import *
import sys
class Winfrom(QWidget):
#定义各种信号
signal1 = pyqtSignal() #无参数的信号
signal2 = pyqtSignal(int) #带一个参数(整型)的信号
signal3 = pyqtSignal(str) #带一个参数(字符串)的信号
signal4 = pyqtSignal(list) #带一个参数(列表)的信号
signal5 = pyqtSignal(dict) #带一个参数(字典)的信号
signal6 = pyqtSignal(int,str) #带两个参数(整数,字符串)的信号
signal7 = pyqtSignal([int,str],[str]) #带两个参数([整数,字符串]或者[字符串])的重载版本的信号
def __init__(self,parent = None):
super().__init__(parent)
self.setWindowTitle('自定义信号和槽函数')
self.resize(400,300)
#将信号和槽连接
self.signal1.connect(self.signal1Call)
self.signal2.connect(self.signal2Call)
self.signal3.connect(self.signal3Call)
self.signal4.connect(self.signal4Call)
self.signal5.connect(self.signal5Call)
self.signal6.connect(self.signal6Call)
self.signal7[int,str].connect(self.signal7Call)
self.signal7[str].connect(self.signal7Calloverload)
#发射信号
self.signal1.emit()
self.signal2.emit(1234)
self.signal3.emit('熊')
self.signal4.emit([1,'xiong',bool])
self.signal5.emit({'xiong':'jt',"":1})
self.signal6.emit(111,'信号和槽')
self.signal7[int,str].emit(1314,'sb')
self.signal7[str].emit('wtfk')
#定义槽函数
def signal1Call(self):
print('signal1 emit')
def signal2Call(self,int1):
print('signal2 emit,value:',int1)
def signal3Call(self,str1):
print('signal3 emit,value:',str1)
def signal4Call(self,list1):
print('signal4 emit,value:',list1)
def signal5Call(self,dict1):
print('signal5 emit,value:',dict1)
def signal6Call(self,int1,str1):
print('signal6 emit,value:',int1,str1)
def signal7Call(self,int1,str1):
print('signal7 emit,value:',int1,str1)
def signal7Calloverload(self,str1):
print('signal7overload emit,value:',str1)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Winfrom()
win.show()
sys.exit(app.exec_())
案例2:
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication ,QWidget ,QHBoxLayout , QPushButton
import sys
class CustWidget( QWidget ):
def __init__(self, parent=None):
super(CustWidget, self).__init__(parent)
self.okButton = QPushButton("OK", self)
#使用setObjectName设置对象名称
self.okButton.setObjectName("okButton")
layout = QHBoxLayout()
layout.addWidget(self.okButton)
self.setLayout(layout)
QtCore.QMetaObject.connectSlotsByName(self)
@QtCore.pyqtSlot()
def on_okButton_clicked(self):
print( "点击了OK按钮")
if __name__ == "__main__":
app = QApplication(sys.argv)
win = CustWidget()
win.show()
sys.exit(app.exec_())
案例3:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(516, 364)
Dialog.setSizeGripEnabled(True)
self.textBrowser = QtWidgets.QTextBrowser(Dialog)
self.textBrowser.setGeometry(QtCore.QRect(25, 20, 471, 192))
self.textBrowser.setObjectName("textBrowser")
self.pushButton1 = QtWidgets.QPushButton(Dialog)
self.pushButton1.setGeometry(QtCore.QRect(90, 250, 91, 31))
font = QtGui.QFont()
font.setFamily("Adobe Myungjo Std M")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.pushButton1.setFont(font)
self.pushButton1.setObjectName("pushButton1")
self.pushButton2 = QtWidgets.QPushButton(Dialog)
self.pushButton2.setGeometry(QtCore.QRect(330, 250, 91, 31))
font = QtGui.QFont()
font.setFamily("Adobe Myungjo Std M")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.pushButton2.setFont(font)
self.pushButton2.setObjectName("pushButton2")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton1.setText(_translate("Dialog", "button1"))
self.pushButton2.setText(_translate("Dialog", "button2"))
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets
from Ui_SignalSlot import Ui_Dialog
class SignalSlot0(QDialog, Ui_Dialog):
btn1_signal = pyqtSignal(int, str)
btn2_signal = pyqtSignal(int, str)
def __init__(self, parent=None):
super(SignalSlot0, self).__init__(parent)
self.setupUi(self)
#self.btn1_signal.connect(self.btn1Call)
#self.btn2_signal.connect(self.btn2Call)
@pyqtSlot()
def on_pushButton1_clicked(self):
"""
Slot documentation goes here.
"""
self.btn1_signal.connect(self.btnCall)
self.btn1_signal.emit(1, '按钮1')
@pyqtSlot()
def on_pushButton2_clicked(self):
"""
Slot documentation goes here.
"""
self.btn2_signal.connect(self.btnCall)
self.btn2_signal.emit(2, '按钮2')
def btnCall(self, int1, str1):
QMessageBox.information(self, 'information notice', 'Button{0} is clicked:{1}'.format(int1, str1), QMessageBox.Yes|QMessageBox.No, QMessageBox.Yes)
def btn2Call(self):
QMessageBox.information(self, 'information notice', 'Button2 clicked')
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ui = SignalSlot0()
ui.show()
sys.exit(app.exec_())