PySide6 GUI 编程(36):查看系统支持的字体及QFont的使用

2024-08-26 19:52:22 浏览数 (3)

示例代码

代码语言:python代码运行次数:0复制
from typing import List

from PySide6.QtGui import QFont, QFontDatabase
from PySide6.QtWidgets import QApplication, QHBoxLayout, QLabel, QLayout, QMainWindow, QScrollArea, QVBoxLayout, QWidget


class MyMainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('支持的字体类型')
        self.font_families = QFontDatabase.families()
        fonts_v_layout = self.init_fonts_layout(self.font_families)
        fonts_area = self.init_scroll_area(fonts_v_layout)
        self.setCentralWidget(fonts_area)

    def init_scroll_area(self, layout: QLayout) -> QScrollArea:
        colors_container = QWidget()
        colors_container.setLayout(layout)
        colors_scroll_area = QScrollArea(self)
        colors_scroll_area.setWidgetResizable(True)
        colors_scroll_area.setWidget(colors_container)
        return colors_scroll_area

    def init_fonts_layout(self, fonts: List[str]) -> QVBoxLayout:
        count = 0
        v_colors_layout = QVBoxLayout()
        h_colors_layout = QHBoxLayout()
        for c in fonts:
            tmp_label = QLabel(c.capitalize(), parent = self)
            tmp_label.setFont(QFont(c, 18))
            h_colors_layout.addWidget(tmp_label)
            count  = 1
            if count % 5 == 0:
                v_colors_layout.addLayout(h_colors_layout)
                h_colors_layout = QHBoxLayout()
                count = 0
        if count > 0:
            v_colors_layout.addLayout(h_colors_layout)
        return v_colors_layout


if __name__ == '__main__':
    app = QApplication()
    ins = MyMainWindow()
    ins.show()
    app.exec()

运行效果

系统字体以及字体效果系统字体以及字体效果

0 人点赞