Qt Creator - 手动关联信号与槽函数

目录

定义新的do_setTextColor()函数

实验效果

附ui_Dialog.py代码


下面代码演示如何设置3个RadioButton按钮的clicked()信号与同一个槽函数关联。

定义新的do_setTextColor()函数

完整代码:注意除了添加了一个函数,也在def __init__(self, parent=None)下面加了3行代码。

import sys
from PyQt6.QtWidgets import QApplication, QDialog
from PyQt6.QtCore import Qt, pyqtSlot
from PyQt6.QtGui import QPalette
from ui_Dialog import Ui_Dialog


class QmyDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.__ui = Ui_Dialog()
        self.__ui.setupUi(self)
        self.__ui.radioButton.clicked.connect(self.do_setTextColor)
        self.__ui.radioButton_2.clicked.connect(self.do_setTextColor)
        self.__ui.radioButton_3.clicked.connect(self.do_setTextColor)

    def do_setTextColor(self):
        plet = self.__ui.plainTextEdit.palette()
        if self.__ui.radioButton.isChecked():
            plet.setColor(QPalette.ColorGroup.Active, QPalette.ColorRole.Text, Qt.GlobalColor.black)
        elif self.__ui.radioButton_2.isChecked():
            plet.setColor(QPalette.ColorGroup.Active, QPalette.ColorRole.Text, Qt.GlobalColor.red)
        elif self.__ui.radioButton_3.isChecked():
            plet.setColor(QPalette.ColorGroup.Active, QPalette.ColorRole.Text, Qt.GlobalColor.blue)
        self.__ui.plainTextEdit.setPalette(plet)

    def on_checkBox_3_toggled(self, checked):
        font = self.__ui.plainTextEdit.font()
        # 参数checked表示勾选状态
        font.setBold(checked)
        self.__ui.plainTextEdit.setFont(font)

    def on_checkBox_clicked(self):
        checked = self.__ui.checkBox.isChecked()
        font = self.__ui.plainTextEdit.font()
        font.setUnderline(checked)
        self.__ui.plainTextEdit.setFont(font)

    @pyqtSlot(bool)
    def on_checkBox_2_clicked(self, checked):
        font = self.__ui.plainTextEdit.font()
        font.setItalic(checked)
        self.__ui.plainTextEdit.setFont(font)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    myDialog = QmyDialog()
    myDialog.show()
    sys.exit(app.exec())

实验效果

 Qt Creator - 手动关联信号与槽函数_第1张图片Qt Creator - 手动关联信号与槽函数_第2张图片

 

附ui_Dialog.py代码

# Form implementation generated from reading ui file 'dialog.ui'
#
# Created by: PyQt6 UI code generator 6.4.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt6 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(564, 434)
        self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
        self.verticalLayout.setObjectName("verticalLayout")
        self.groupBox_2 = QtWidgets.QGroupBox(Dialog)
        self.groupBox_2.setObjectName("groupBox_2")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.groupBox_2)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.checkBox = QtWidgets.QCheckBox(self.groupBox_2)
        self.checkBox.setObjectName("checkBox")
        self.horizontalLayout.addWidget(self.checkBox)
        self.checkBox_2 = QtWidgets.QCheckBox(self.groupBox_2)
        self.checkBox_2.setObjectName("checkBox_2")
        self.horizontalLayout.addWidget(self.checkBox_2)
        self.checkBox_3 = QtWidgets.QCheckBox(self.groupBox_2)
        self.checkBox_3.setObjectName("checkBox_3")
        self.horizontalLayout.addWidget(self.checkBox_3)
        self.verticalLayout.addWidget(self.groupBox_2)
        self.groupBox = QtWidgets.QGroupBox(Dialog)
        self.groupBox.setObjectName("groupBox")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.groupBox)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.radioButton = QtWidgets.QRadioButton(self.groupBox)
        self.radioButton.setObjectName("radioButton")
        self.horizontalLayout_2.addWidget(self.radioButton)
        self.radioButton_3 = QtWidgets.QRadioButton(self.groupBox)
        self.radioButton_3.setObjectName("radioButton_3")
        self.horizontalLayout_2.addWidget(self.radioButton_3)
        self.radioButton_2 = QtWidgets.QRadioButton(self.groupBox)
        self.radioButton_2.setObjectName("radioButton_2")
        self.horizontalLayout_2.addWidget(self.radioButton_2)
        self.verticalLayout.addWidget(self.groupBox)
        self.plainTextEdit = QtWidgets.QPlainTextEdit(Dialog)
        font = QtGui.QFont()
        font.setPointSize(20)
        font.setBold(True)
        self.plainTextEdit.setFont(font)
        self.plainTextEdit.setObjectName("plainTextEdit")
        self.verticalLayout.addWidget(self.plainTextEdit)
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)
        self.horizontalLayout_3.addItem(spacerItem)
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout_3.addWidget(self.pushButton)
        spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)
        self.horizontalLayout_3.addItem(spacerItem1)
        self.pushButton_2 = QtWidgets.QPushButton(Dialog)
        self.pushButton_2.setObjectName("pushButton_2")
        self.horizontalLayout_3.addWidget(self.pushButton_2)
        self.pushButton_3 = QtWidgets.QPushButton(Dialog)
        self.pushButton_3.setObjectName("pushButton_3")
        self.horizontalLayout_3.addWidget(self.pushButton_3)
        self.verticalLayout.addLayout(self.horizontalLayout_3)

        self.retranslateUi(Dialog)
        self.pushButton_2.clicked.connect(Dialog.accept) # type: ignore
        self.pushButton_3.clicked.connect(Dialog.close) # type: ignore
        self.pushButton.clicked.connect(self.plainTextEdit.clear) # type: ignore
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Demo2-3信号与槽"))
        self.groupBox_2.setTitle(_translate("Dialog", "GroupBox"))
        self.checkBox.setText(_translate("Dialog", "Underline"))
        self.checkBox_2.setText(_translate("Dialog", "Italic"))
        self.checkBox_3.setText(_translate("Dialog", "Bold"))
        self.groupBox.setTitle(_translate("Dialog", "GroupBox"))
        self.radioButton.setText(_translate("Dialog", "Black"))
        self.radioButton_3.setText(_translate("Dialog", "Blue"))
        self.radioButton_2.setText(_translate("Dialog", "Red"))
        self.plainTextEdit.setPlainText(_translate("Dialog", "PyQt6编程指南\n"
"Python和Qt."))
        self.pushButton.setText(_translate("Dialog", "清空"))
        self.pushButton_2.setText(_translate("Dialog", "确定"))
        self.pushButton_3.setText(_translate("Dialog", "退出"))

你可能感兴趣的:(Python,qt,ui,开发语言)