PyQT5:信号和槽

PyQT5:信号和槽

信号和槽

Qt的主要特征之一是它使用信号和插槽在对象之间进行通信。

当潜在的事件发生时,会发出一个信号。插槽是可调用的Python,如果将信号连接到插槽,则在发出信号时将调用该插槽。如果没有连接信号,则什么也不会发生。

信号/插槽机制具有以下功能。

  1. 信号可能连接到许多插槽。
  2. 一个信号也可以连接到另一个信号。
  3. 信号参数可以是任何Python类型。
  4. 插槽可以连接到许多信号。
  5. 连接可以是直接的(同步的)或排队的(异步的)。
  6. 可以跨线程进行连接。
  7. 信号可能断开。

信号(特别是未绑定的信号)是类属性。当信号被引用为该类的实例的属性时,PyQt5会自动将该实例绑定到该信号,以创建绑定信号。这与Python本身用于从类函数创建绑定方法的机制相同。

一个绑定信号具有connect()disconnect()emit()实现相关联的功能的方法。它还具有一个signal属性,该属性是将由Qt的SIGNAL() 宏返回的信号的签名。

5种不同的信号

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file ‘ParaTrans.ui‘
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.
import random
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QObject, pyqtSignal, QThread
from PyQt5.QtWidgets import *


class Ui_MainWindow(QWidget):
    signal1 = pyqtSignal(str)
    signal2 = pyqtSignal(int, str)
    signal3 = pyqtSignal(list)
    signal4 = pyqtSignal(dict)
    signal5 = pyqtSignal([int, str], [str])

    def setupUi(self, MainWindow):

        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 800)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
        self.plainTextEdit.setGeometry(QtCore.QRect(240, 150, 500, 500))
        self.plainTextEdit.setObjectName("plainTextEdit")
        self.widget = QtWidgets.QWidget(self.centralwidget)
        self.widget.setGeometry(QtCore.QRect(20, 140, 200, 400))
        self.widget.setObjectName("widget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.pushButton = QtWidgets.QPushButton(self.widget)
        self.pushButton.setObjectName("pushButton")
        self.verticalLayout.addWidget(self.pushButton)
        self.pushButton_2 = QtWidgets.QPushButton(self.widget)
        self.pushButton_2.setObjectName("pushButton_2")
        self.verticalLayout.addWidget(self.pushButton_2)
        self.pushButton_3 = QtWidgets.QPushButton(self.widget)
        self.pushButton_3.setObjectName("pushButton_3")
        self.verticalLayout.addWidget(self.pushButton_3)
        self.pushButton_4 = QtWidgets.QPushButton(self.widget)
        self.pushButton_4.setObjectName("pushButton_4")
        self.verticalLayout.addWidget(self.pushButton_4)
        self.pushButton_5 = QtWidgets.QPushButton(self.widget)
        self.pushButton_5.setObjectName("pushButton_5")
        #信号和槽
        self.pushButton.clicked.connect(self.mysignal1)
        self.signal1.connect(self.mySlotFunc1)
        self.pushButton_2.clicked.connect(self.mysignal2)
        self.signal2.connect(self.mySlotFunc2)
        self.pushButton_3.clicked.connect(self.mysignal3)
        self.signal3.connect(self.mySlotFunc3)
        self.pushButton_4.clicked.connect(self.mysignal4)
        self.signal4.connect(self.mySlotFunc4)
        self.pushButton_5.clicked.connect(self.mysignal5)
        self.signal5[int, str].connect(self.mySlotFunc5_1)
        self.signal5[str].connect(self.mySlotFunc5_2)

        self.verticalLayout.addWidget(self.pushButton_5)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def mysignal1(self):
        self.signal1.emit("测试str类型")

    def mysignal2(self):
        self.signal2.emit(123456, "      测试int+str类型")


    def mysignal3(self):
        list = [1,2,3,4,5,6,7,8,9]
        self.signal3.emit(list)

    def mysignal4(self):
        dic = {"北京":"beijing","上海":"shanghai","广州":"guangzhou","深圳":"shenzhen"}
        self.signal4.emit(dic)
    #([int, str], [str])
    # 声明一个重载版本的信号,也就是槽函数的参数可以是int和str类型,也可以只有一个str类型的参数

    def mysignal5(self):
        flag = random.random() < 0.5
        if flag == True:
            self.signal5[int, str].emit(999, "重载参数类型:[int, str]")
        elif flag == False:
            self.signal5[str].emit("重载参数类型:[str]")

    #######################
    def mySlotFunc1(self,val):
        self.plainTextEdit.setPlainText(val)
        print("hello")

    def mySlotFunc2(self,intval,text):
        content = str(intval)+text
        self.plainTextEdit.setPlainText(content)

    def mySlotFunc3(self,list):
        print(str(list))
        self.plainTextEdit.setPlainText(str(list))

    def mySlotFunc4(self,dic):
        self.plainTextEdit.setPlainText(str(dic))

    def mySlotFunc5_1(self, val1, val2):
        self.plainTextEdit.setPlainText(str(val1)+val2)

    def mySlotFunc5_2(self, val):
        self.plainTextEdit.setPlainText(str(val))


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "str测试"))
        self.pushButton_2.setText(_translate("MainWindow", "str int测试"))
        self.pushButton_3.setText(_translate("MainWindow", "list测试"))
        self.pushButton_4.setText(_translate("MainWindow", "dict测试"))
        self.pushButton_5.setText(_translate("MainWindow", "重载信号测试"))

if __name__ == ‘__main__‘:
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    Ui = Ui_MainWindow()
    # 向主窗口上添加控件
    Ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

PyQT5:信号和槽

上一篇:Apoll


下一篇:Jenkins 学习路上遇到的坑