使用QT设计师-信号和槽signal-slot(第一弹)
自定义信号和槽的步骤:
1、定义信号---signal1 = pyqtSignal()
2、定义槽信号---def setSlot(self):
3、连接信号和槽函数---signal1.connect(self.setSlot)
4、发射信号---signal1.emit()
案例1:
1 from PyQt5.QtCore import QObject,pyqtSignal 2 from PyQt5.QtWidgets import * 3 import sys 4 5 class Winfrom(QWidget): 6 #定义各种信号 7 signal1 = pyqtSignal() #无参数的信号 8 signal2 = pyqtSignal(int) #带一个参数(整型)的信号 9 signal3 = pyqtSignal(str) #带一个参数(字符串)的信号 10 signal4 = pyqtSignal(list) #带一个参数(列表)的信号 11 signal5 = pyqtSignal(dict) #带一个参数(字典)的信号 12 signal6 = pyqtSignal(int,str) #带两个参数(整数,字符串)的信号 13 signal7 = pyqtSignal([int,str],[str]) #带两个参数([整数,字符串]或者[字符串])的重载版本的信号 14 15 def __init__(self,parent = None): 16 super().__init__(parent) 17 self.setWindowTitle('自定义信号和槽函数') 18 self.resize(400,300) 19 #将信号和槽连接 20 self.signal1.connect(self.signal1Call) 21 self.signal2.connect(self.signal2Call) 22 self.signal3.connect(self.signal3Call) 23 self.signal4.connect(self.signal4Call) 24 self.signal5.connect(self.signal5Call) 25 self.signal6.connect(self.signal6Call) 26 self.signal7[int,str].connect(self.signal7Call) 27 self.signal7[str].connect(self.signal7Calloverload) 28 29 #发射信号 30 self.signal1.emit() 31 self.signal2.emit(1234) 32 self.signal3.emit('熊') 33 self.signal4.emit([1,'xiong',bool]) 34 self.signal5.emit({'xiong':'jt',"":1}) 35 self.signal6.emit(111,'信号和槽') 36 self.signal7[int,str].emit(1314,'sb') 37 self.signal7[str].emit('wtfk') 38 39 #定义槽函数 40 def signal1Call(self): 41 print('signal1 emit') 42 43 def signal2Call(self,int1): 44 print('signal2 emit,value:',int1) 45 46 def signal3Call(self,str1): 47 print('signal3 emit,value:',str1) 48 49 def signal4Call(self,list1): 50 print('signal4 emit,value:',list1) 51 52 def signal5Call(self,dict1): 53 print('signal5 emit,value:',dict1) 54 55 def signal6Call(self,int1,str1): 56 print('signal6 emit,value:',int1,str1) 57 58 def signal7Call(self,int1,str1): 59 print('signal7 emit,value:',int1,str1) 60 61 def signal7Calloverload(self,str1): 62 print('signal7overload emit,value:',str1) 63 64 if __name__ == '__main__': 65 app = QApplication(sys.argv) 66 win = Winfrom() 67 win.show() 68 sys.exit(app.exec_())
案例2:
1 from PyQt5 import QtCore 2 from PyQt5.QtWidgets import QApplication ,QWidget ,QHBoxLayout , QPushButton 3 import sys 4 5 class CustWidget( QWidget ): 6 7 def __init__(self, parent=None): 8 super(CustWidget, self).__init__(parent) 9 10 self.okButton = QPushButton("OK", self) 11 #使用setObjectName设置对象名称 12 self.okButton.setObjectName("okButton") 13 layout = QHBoxLayout() 14 layout.addWidget(self.okButton) 15 self.setLayout(layout) 16 QtCore.QMetaObject.connectSlotsByName(self) 17 18 @QtCore.pyqtSlot() 19 def on_okButton_clicked(self): 20 print( "点击了OK按钮") 21 22 23 if __name__ == "__main__": 24 app = QApplication(sys.argv) 25 win = CustWidget() 26 win.show() 27 sys.exit(app.exec_())
案例3:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 from PyQt5 import QtCore, QtGui, QtWidgets 2 3 class Ui_Dialog(object): 4 def setupUi(self, Dialog): 5 Dialog.setObjectName("Dialog") 6 Dialog.resize(516, 364) 7 Dialog.setSizeGripEnabled(True) 8 self.textBrowser = QtWidgets.QTextBrowser(Dialog) 9 self.textBrowser.setGeometry(QtCore.QRect(25, 20, 471, 192)) 10 self.textBrowser.setObjectName("textBrowser") 11 self.pushButton1 = QtWidgets.QPushButton(Dialog) 12 self.pushButton1.setGeometry(QtCore.QRect(90, 250, 91, 31)) 13 font = QtGui.QFont() 14 font.setFamily("Adobe Myungjo Std M") 15 font.setPointSize(12) 16 font.setBold(True) 17 font.setWeight(75) 18 self.pushButton1.setFont(font) 19 self.pushButton1.setObjectName("pushButton1") 20 self.pushButton2 = QtWidgets.QPushButton(Dialog) 21 self.pushButton2.setGeometry(QtCore.QRect(330, 250, 91, 31)) 22 font = QtGui.QFont() 23 font.setFamily("Adobe Myungjo Std M") 24 font.setPointSize(12) 25 font.setBold(True) 26 font.setWeight(75) 27 self.pushButton2.setFont(font) 28 self.pushButton2.setObjectName("pushButton2") 29 30 self.retranslateUi(Dialog) 31 QtCore.QMetaObject.connectSlotsByName(Dialog) 32 33 def retranslateUi(self, Dialog): 34 _translate = QtCore.QCoreApplication.translate 35 Dialog.setWindowTitle(_translate("Dialog", "Dialog")) 36 self.pushButton1.setText(_translate("Dialog", "button1")) 37 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_())