如何使用QFontDialog预览非系统字体

How to use QFontDialog to preview non-system fonts

我想使用 QFontDialog 小部件在安装之前预览外部字体。但是,默认情况下,QFontDialog 显然只列出已安装的系统字体。

您不能指定自定义字体文件夹,但可以使用 QFontDatabase class 添加单独的字体。因此,您需要做的就是遍历给定文件夹中的文件并添加它包含的任何字体文件。文档记录了这些限制:

Currently only TrueType fonts, TrueType font collections, and OpenType fonts are supported.

Note: Adding application fonts on Unix/X11 platforms without fontconfig is currently not supported.

添加所有有效字体文件后,它们将立即显示在 font-dialog 中。这是一个简单的演示(仅在 Linux 上测试):

import sys, os, glob
from PyQt5 import QtCore, QtGui, QtWidgets

class Window(QtWidgets.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.button1 = QtWidgets.QPushButton('Open Font Folder')
        self.button1.clicked.connect(self.handleButton1)
        self.button2 = QtWidgets.QPushButton('Show Font Dialog')
        self.button2.clicked.connect(self.handleButton2)
        self.fontList = QtWidgets.QListWidget()
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.fontList)
        layout.addWidget(self.button1)
        layout.addWidget(self.button2)

    def handleButton1(self):
        path = QtWidgets.QFileDialog.getExistingDirectory(self)
        if path:
            fonts = set()
            self.fontList.clear()
            db = QtGui.QFontDatabase()
            db.removeAllApplicationFonts()
            for filename in glob.glob(os.path.join(path, '*.ttf')):
                fontid = db.addApplicationFont(os.path.join(path, filename))
                if fontid >= 0:
                    fonts.update(db.applicationFontFamilies(fontid))
            self.fontList.addItems(sorted(fonts))
            self.fontList.setCurrentRow(0)

    def handleButton2(self):
        font = QtGui.QFont()
        item = self.fontList.currentItem()
        if item is not None:
            font.setFamily(item.text())
        QtWidgets.QFontDialog.getFont(font, self)

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.setGeometry(600, 100, 200, 200)
    window.show()
    sys.exit(app.exec_())