加载外部字体,但是会稍微影响开启速度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <QApplication>
#include <QStyleFactory>
#include <QFontDataBase>
#include <QIODevice>

QString loadFontFamilyFromFiles(const QString &fontFilePath)
{
QString font = "";

QFile fontFile(fontFilePath);
if(!fontFile.open(QIODevice::ReadOnly))
{
qDebug()<<"Open font file error";
return font;
}

int loadedFontID = QFontDatabase::addApplicationFontFromData(fontFile.readAll());
QStringList loadedFontFamilies = QFontDatabase::applicationFontFamilies(loadedFontID);
if(!loadedFontFamilies.empty())
{
font = loadedFontFamilies.at(0);
}
fontFile.close();
return font;
}

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QString font_path = "字体路径(ttf/otf)";
if (isFileExist(font_path))
{
QString strFont = loadFontFamilyFromFiles(font_path);
QFont font(strFont, 9);
a.setFont(font);
}

MainWindow w;
w.show();

return a.exec();
};