如何使用QFontDialog预览非系统字体?能否指定自定义字体文件夹?
Great question! Let’s tackle this for you since I’ve dealt with similar font preview needs in Qt projects before.
Unfortunately, QFontDialog doesn’t have a built-in setting to directly point to a custom font folder. But here’s a workaround that works perfectly for previewing external fonts without installing them: you can temporarily load the fonts from your custom folder into Qt’s font database, and QFontDialog will automatically pick them up.
The key here is that these fonts are only loaded for your application’s session—they won’t be installed system-wide, which is exactly what you need for previewing. Here’s how to implement it in C++:
#include <QFontDatabase> #include <QDir> #include <QFontDialog> // Step 1: Load all font files from your custom folder QString customFontDir = "/path/to/your/fonts/folder"; // Replace with your path QDir fontDir(customFontDir); // Filter common font file types QStringList fontFilters = {"*.ttf", "*.otf", "*.ttc", "*.woff", "*.woff2"}; QFileInfoList fontFiles = fontDir.entryInfoList(fontFilters, QDir::Files); // Load each font into the application's font database for (const auto& fileInfo : fontFiles) { int fontId = QFontDatabase::addApplicationFont(fileInfo.absoluteFilePath()); if (fontId == -1) { qWarning() << "Failed to load font:" << fileInfo.fileName(); } } // Step 2: Open QFontDialog as usual—it will now include your custom fonts bool fontSelected; QFont selectedFont = QFontDialog::getFont(&fontSelected, QFont(), this, "Preview Custom Fonts"); if (fontSelected) { // Use the selected font (e.g., apply it to a label) // ui->previewLabel->setFont(selectedFont); }
This approach leverages Qt’s existing font dialog, so you don’t have to rebuild all the standard font selection features (like adjusting size, style, etc.).
If you want full control over the preview UI (like letting users input custom text, or having a more integrated preview panel), building your own with basic Qt components is a great option. Here are two common approaches:
1. ComboBox + Label (Simple Preview)
Create a dropdown of font families and a preview label that updates when the user selects a font:
#include <QComboBox> #include <QLabel> #include <QVBoxLayout> // After loading fonts into the database (same as above) QStringList allFontFamilies = QFontDatabase::families(QFontDatabase::All); QComboBox* fontSelector = new QComboBox(this); fontSelector->addItems(allFontFamilies); QLabel* previewLabel = new QLabel("The quick brown fox jumps over the lazy dog", this); previewLabel->setFixedSize(400, 120); previewLabel->setStyleSheet("border: 1px solid #ddd; padding: 15px;"); previewLabel->setWordWrap(true); // Connect selector to preview update connect(fontSelector, &QComboBox::currentTextChanged, this, [=](const QString& fontFamily) { QFont previewFont(fontFamily, 16); previewLabel->setFont(previewFont); }); // Layout the widgets QVBoxLayout* layout = new QVBoxLayout(this); layout->addWidget(fontSelector); layout->addWidget(previewLabel); setLayout(layout);
2. Text Edit for Custom Preview Text
For a more interactive experience, use a QTextEdit instead of a label—users can type their own text to see how the font looks with content relevant to them:
#include <QTextEdit> // Replace the QLabel with QTextEdit QTextEdit* previewEdit = new QTextEdit("Type your own preview text here...", this); previewEdit->setFixedSize(400, 150); connect(fontSelector, &QComboBox::currentTextChanged, this, [=](const QString& fontFamily) { QFont previewFont(fontFamily, 14); previewEdit->setFont(previewFont); });
For Qt Quick projects, you can use FontLoader to load individual fonts and bind them to a Text component for preview—this is equally straightforward and flexible.
内容的提问来源于stack exchange,提问作者Nemo XXX




