码迷,mamicode.com
首页 > 其他好文 > 详细

【PyQt5-Qt Designer】pyqtSignal()-高级自定义信号与槽

时间:2019-01-04 23:33:19      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:code   get   show   nbsp   cli   connect   数字信号   类型   +=   

PyQt 5信号与槽的几种高级玩法

参考:http://www.broadview.com.cn/article/824

from PyQt5.QtCore import QObject , pyqtSignal

class CustSignal(QObject):

    #声明无参数的信号
    signal1 = pyqtSignal()

    #声明带一个int类型参数的信号
    signal2 = pyqtSignal(int)

    #声明带int和str类型参数的信号
    signal3 = pyqtSignal(int,str)

    #声明带一个列表类型参数的信号
    signal4 = pyqtSignal(list)

    #声明带一个字典类型参数的信号
    signal5 = pyqtSignal(dict)

    #声明一个多重载版本的信号,包括带int和str类型参数的信号和带str类型参数的信号
    signal6 = pyqtSignal([int,str], [str])

    def __init__(self,parent=None):
        super(CustSignal,self).__init__(parent)

        #将信号连接到指定槽函数
        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[int,str].connect(self.signalCall6)
        self.signal6[str].connect(self.signalCall6OverLoad)

        #发射信号
        self.signal1.emit()
        self.signal2.emit(1)
        self.signal3.emit(1,"text")
        self.signal4.emit([1,2,3,4])
        self.signal5.emit({"name":"wangwu","age":"25"})
        self.signal6[int,str].emit(1,"text")
        self.signal6[str].emit("text")

    def signalCall1(self):
        print("signal1 emit")

    def signalCall2(self,val):
        print("signal2 emit,value:",val)

    def signalCall3(self,val,text):
        print("signal3 emit,value:",val,text)

    def signalCall4(self,val):
        print("signal4 emit,value:",val)

    def signalCall5(self,val):
        print("signal5 emit,value:",val)

    def signalCall6(self,val,text):
        print("signal6 emit,value:",val,text)

    def signalCall6OverLoad(self,val):
        print("signal6 overload emit,value:",val)

if __name__ == __main__:  
    custSignal = CustSignal()

运行结果:

signal1 emit
signal2 emit,value: 1
signal3 emit,value: 1 text
signal4 emit,value: [1, 2, 3, 4]
signal5 emit,value: {name: wangwu, age: 25}
signal6 emit,value: 1 text
signal6 overload emit,value: text

自己写了一遍:

效果如下:

技术分享图片

完整代码:

from PyQt5.QtWidgets import (QApplication,QWidget,QPushButton,QTextBrowser,QGridLayout)
from PyQt5.QtCore import Qt,pyqtSignal
import sys

class Example(QWidget):
    # 声明无参数的信号
    signal1 = pyqtSignal()
    # 声明带一个int类型参数的信号
    signal2 = pyqtSignal(int)
    # 声明带int和str类型参数的信号
    signal3 = pyqtSignal(int, str)
    # 声明带一个列表类型参数的信号
    signal4 = pyqtSignal(list)
    # 声明带一个字典类型参数的信号
    signal5 = pyqtSignal(dict)
    # 声明一个多重载版本的信号,包括带int和str类型参数的信号和带str类型参数的信号
    signal6 = pyqtSignal([int, str], [str])

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(300,300,450,380)
        self.setWindowTitle("自定义信号与槽练习")
        gridLayout = QGridLayout()
        self.btn1 = QPushButton("无参数信号")
        self.btn2 = QPushButton("int信号")
        self.btn3 = QPushButton("int和str信号")
        self.btn4 = QPushButton("list信号")
        self.btn5 = QPushButton("dict信号")
        self.btn6 = QPushButton("多重载信号[int,str],[str]")
        self.textBrowser = QTextBrowser()
        gridLayout.addWidget(self.textBrowser,0,0,4,12)
        gridLayout.addWidget(self.btn1,5,0,1,2)
        gridLayout.addWidget(self.btn2,5,2,1,2)
        gridLayout.addWidget(self.btn3,5,4,1,2)
        gridLayout.addWidget(self.btn4,5,6,1,2)
        gridLayout.addWidget(self.btn5,5,8,1,2)
        gridLayout.addWidget(self.btn6,5,10,1,2)
        self.setLayout(gridLayout)
        self.mytxt = ""
        #空信号
        self.btn1.clicked.connect(self.mySignal1)
        self.signal1.connect(self.mySlotFunc1)
        #int数字信号
        self.btn2.clicked.connect(self.mySignal2)
        self.signal2.connect(self.mySlotFunc2)
        #int和str(数字和字符串)信号
        self.btn3.clicked.connect(self.mySignal3)
        self.signal3.connect(self.mySlotFunc3)
        #list列表信号
        self.btn4.clicked.connect(self.mySignal4)
        self.signal4.connect(self.mySlotFunc4)
        #dict字典信号
        self.btn5.clicked.connect(self.mySignal5)
        self.signal5.connect(self.mySlotFunc5)
        # 多重载信号

    def mySignal1(self):
        self.signal1.emit()
    def mySlotFunc1(self):
        self.mytxt += "无参数的信号"
        self.textBrowser.setText(self.mytxt)

    def mySignal2(self):
        self.signal2.emit(123456)

    def mySlotFunc2(self,val):
        self.textBrowser.setText(str(val))

    def mySignal3(self):
        self.signal3.emit(123456,"这是我的电话号码:")

    def mySlotFunc3(self, val,text):
        self.textBrowser.setText(text+str(val))

    def mySignal4(self):
        self.signal4.emit([1,5,9,0,0,0,0,1,2,3,4])

    def mySlotFunc4(self,li):
        print(li)
        for i in li:
            self.mytxt += str(i)
            # print(self.mytxt)
        self.textBrowser.setText(self.mytxt)

    def mySignal5(self):
        self.signal5.emit({"phone":90001111,
                           "addr":["浙江","宁波"]})

    def mySlotFunc5(self,mydict):
        self.textBrowser.setText(str(mydict["phone"])+str(mydict["addr"][0])+str(mydict["addr"][1]))


if __name__ == __main__:
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

 

【PyQt5-Qt Designer】pyqtSignal()-高级自定义信号与槽

标签:code   get   show   nbsp   cli   connect   数字信号   类型   +=   

原文地址:https://www.cnblogs.com/XJT2018/p/10222981.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!