转自:http://blog.csdn.net/hlqyq/article/details/6713828
import sys
from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt
from PyQt5.QtWidgets import QWidget, QApplication class MyWidget(QWidget): Signal_NoParameters = pyqtSignal() Signal_OneParameter = pyqtSignal(int) Signal_OneParameter_Overload =pyqtSignal([int],[str]) Signal_TwoParameters = pyqtSignal(int,str) Signal_TwoParameters_Overload =pyqtSignal([int,int],[int,str]) def mousePressEvent(self, event): self.Signal_NoParameters.emit() self.Signal_OneParameter.emit(1) self.Signal_OneParameter_Overload.emit(1) self.Signal_OneParameter_Overload[str].emit("abc") self.Signal_TwoParameters.emit(1,"abc") self.Signal_TwoParameters_Overload.emit(1,2) self.Signal_TwoParameters_Overload[int, str].emit(1,"abc") @pyqtSlot() def setValue_NoParameters(self): print('''无参数槽方法''') pass @pyqtSlot(int) def setValue_OneParameter(self,nIndex): print( '''一个参数(整数)槽方法''', nIndex) pass @pyqtSlot(str) def setValue_OneParameter_String(self,szIndex): print( '''一个参数(字符串)的槽方法''', szIndex) pass ##@pyqtSlot(int,int) def setValue_TwoParameters(self,x,y): print( '二个参数(整数,整数)槽方法', x, y) pass ## @pyqtSlot(int,str) 加不加槽修饰没看出来啥区别。。。 def setValue_TwoParameters_String(self,x,yx): print('二个参数(整数,字符串)槽方法', x, yx) pass app = QApplication(sys.argv) widget = MyWidget() widget.show() widget.Signal_NoParameters.connect(widget.setValue_NoParameters,Qt.QueuedConnection) widget.Signal_OneParameter.connect(widget.setValue_OneParameter,Qt.QueuedConnection) widget.Signal_OneParameter_Overload[int].connect(widget.setValue_OneParameter,Qt.QueuedConnection) widget.Signal_OneParameter_Overload[str].connect(widget.setValue_OneParameter_String,Qt.QueuedConnection) widget.Signal_TwoParameters.connect(widget.setValue_TwoParameters,Qt.QueuedConnection) widget.Signal_TwoParameters_Overload[int,int].connect(widget.setValue_TwoParameters,Qt.QueuedConnection) widget.Signal_TwoParameters_Overload[int,str].connect(widget.setValue_TwoParameters_String,Qt.QueuedConnection) sys.exit(app.exec_())
signal即可以和solit连接,也可以和signal连接。