前面已经用过很多,下面简单举个例子回顾一下。
代码:
import sys,math
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Demo(QWidget):
def __init__(self):
super(Demo, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(300,300,300,100)
self.setWindowTitle('信号(Signal)与槽(Slot)')
self.btn=QPushButton('我的按钮',self)
self.btn.clicked.connect(self.onClick)
def onClick(self):
self.btn.setText('信号已经发出')
self.btn.setStyleSheet('QPushButton(max-width:200px;min-width:200px)')
if __name__=='__main__':
app=QApplication(sys.argv)
main=Demo()
main.show()
sys.exit(app.exec_())
运行结果:
代码:
import sys,math
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class MyTypeSignal(QObject):
#定义一个信号
sendmsg=pyqtSignal(object)
#调用run来实现触发
def run(self):
self.sendmsg.emit('Hello PyQt5')#给槽传递一个参数
class MySlot(QObject):
#槽函数
def get(self,msg):
print('信息:'+msg)
if __name__=='__main__':
send=MyTypeSignal()
slot=MySlot()
send.sendmsg.connect(slot.get)#连接信号与槽函数
send.run()#发送信号
#断开信号与槽的连接
#send.sendmsg.disconnect(slot.get)
运行结果:
代码:
import sys,math
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class MyTypeSignal(QObject):
#定义一个信号
sendmsg=pyqtSignal(object)
#发送三个参数的信号
sendmsg1=pyqtSignal(str,int,int)
#调用run来实现触发
def run(self):
self.sendmsg.emit('Hello PyQt5')#给槽传递一个参数
def run1(self):
self.sendmsg1.emit('hello',3,4)
class MySlot(QObject):
#槽函数
def get(self,msg):
print('信息:'+msg)
def get1(self,msg,a,b):
print(msg)
print(a+b)
if __name__=='__main__':
send=MyTypeSignal()
slot=MySlot()
send.sendmsg.connect(slot.get)#连接信号与槽函数
send.sendmsg1.connect(slot.get1)
send.run()#发送信号
send.run1()#别忘了调用相应的触发函数!!!!
#断开信号与槽的连接
#send.sendmsg.disconnect(slot.get)
运行结果:
代码:
import sys,math
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class MultiSignal(QObject):
#无参数
signal1=pyqtSignal()
#有一个整型的参数
signal2 = pyqtSignal(int)
#参数一是整型,参数二是字符型
signal3 = pyqtSignal(int,str)
#参数是一个列表
signal4 = pyqtSignal(list)
# 参数是一个字典
signal5 = pyqtSignal(dict)
#声明一个重载版本的信号,中括号之间是或的意思,也就是槽函数可以是两个参数:int和str类型,也可以是一个参数:str类型
signal6 = pyqtSignal([int,str],[str])
def __init__(self):
super(MultiSignal, self).__init__()
self.signal1.connect(self.signalcall1)
self.signal2.connect(self.signalcall2)
self.signal3.connect(self.signalcall3)
self.signal4.connect(self.signalcall4)
self.signal5.connect(self.signalcall5)
#self.signal6.connect(self.signalcall6)#默认关联到重构的第一个槽函数上
# 为了可读性,可像下面那样写
self.signal6[str].connect(self.signalcall6Overload)
self.signal6[int, str].connect(self.signalcall6)
self.signal1.emit()
self.signal2.emit(10)
self.signal3.emit(1,'hello world')
self.signal4.emit([1,2,3,4,5])
self.signal5.emit({'name':'Bill','age':30})
self.signal6[str].emit('test')
self.signal6[int, str].emit(20,'mytest')
#槽函数
def signalcall1(self):
print('signal1 emit')
def signalcall2(self,val):
print('signa2 emit,value:',val)
def signalcall3(self,val,text):
print('signa3 emit,value:',val,text)
def signalcall4(self,val):
print('signa4 emit,value:',val)
def signalcall5(self,val):
print('signa5 emit,value:',val)
def signalcall6(self,val,text):
print('signa6 emit,value:',val,text)
def signalcall6Overload(self,val):
print('signa6 overload emit,value:',val)
if __name__=='__main__':
multiSignal=MultiSignal()
运行结果:
代码:
import sys,math
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class NNSignal(QObject):
signal1=pyqtSignal()#无参数
signal2=pyqtSignal(int)
signal3=pyqtSignal()
def __init__(self):
super(NNSignal, self).__init__()
#一个信号对应多个槽
self.signal1.connect(self.call1)
self.signal1.connect(self.call11)
self.signal1.emit()
#信号绑定到信号上
self.signal2.connect(self.signal1)
print('*****************************')
self.signal2.emit(2)#触发了信号1
'''
以上操作的运行结果
call1 emit
call11 emit
call1 emit
call11 emit
'''
print('************************************')
#解绑
self.signal1.disconnect(self.call1)
self.signal1.disconnect(self.call11)
self.signal2.disconnect(self.signal1)
#重新绑定
self.signal1.connect(self.call1)
self.signal2.connect(self.call2)
#一个槽函数绑定到多个信号上
self.signal3.connect(self.call1)
self.signal1.emit()
self.signal2.emit(100)
self.signal3.emit()
def call1(self):
print('call1 emit')
def call11(self):
print('call11 emit')
def call2(self,val):
print('call2 emit',val)
if __name__=='__main__':
nnSignal=NNSignal()
运行结果:
call1 emit
call11 emit
*****************************
call1 emit
call11 emit
************************************
call1 emit
call2 emit 100
call1 emit