FontComboBox
关键代码
# connect signal
self.fontComboBox.currentFontChanged.connect(self.change_type)
# 如果ComboBox中,currentFontChanged,那么发送信号,然后获取到Index,通过Index获取当前字体名,然后通过QFont更改字体
myFont = QFont(self.fontComboBox.itemText(self.fontComboBox.currentIndex()))
完整代码
# Form implementation generated from reading ui file 'FontComboBox.ui'
#
# Created by: PyQt6 UI code generator 6.3.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
from PyQt6.QtGui import QFont
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(Form)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.fontComboBox = QtWidgets.QFontComboBox(Form)
self.fontComboBox.setObjectName("fontComboBox")
# connect signal
self.fontComboBox.currentFontChanged.connect(self.change_type)
self.horizontalLayout.addWidget(self.fontComboBox)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)
self.horizontalLayout_2.addItem(spacerItem)
self.textEdit = QtWidgets.QTextEdit(Form)
self.textEdit.setObjectName("textEdit")
self.horizontalLayout_2.addWidget(self.textEdit)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def change_type(self):
myFont = QFont(self.fontComboBox.itemText(self.fontComboBox.currentIndex()))
self.textEdit.setFont(myFont)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label.setText(_translate("Form", "font"))
self.label_2.setText(_translate("Form", "textTable"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec())
程序截图