qt使用本地字体文件
qml工程设置使用本地字体文件
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QDebug>
#include <QFontDatabase>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
#if 1
QFontDatabase database;
QString fontPath = "/opt/font/default.ttf";
int fontId = QFontDatabase::addApplicationFont(fontPath);
int count = 0;
qWarning() << "applicationFontFamilies: " << QFontDatabase::applicationFontFamilies(fontId);
foreach (const QString &family, database.families())
{
qWarning() << count++ << ": " << family;
foreach (const QString &strStyle, database.styles(family))
{
QString strSizes;
foreach (int points, database.smoothSizes(family, strStyle))
{
strSizes += QString::number(points) + " ";
}
qDebug() << "\t" << strStyle.toStdString().c_str() << "\t" << strSizes.toStdString().c_str();
}
}
if(fontId >= 0)
{
QGuiApplication::setFont(QFont(QFontDatabase::applicationFontFamilies(fontId).at(0)));
}
else
{
qWarning() << QString("无法使用字体文件%1").arg(fontPath);
QGuiApplication::setFont(QFont("思源黑体"));
}
#endif
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
标签:qt,int,QFontDatabase,字体,本地,include
From: https://www.cnblogs.com/yuanhaoblog/p/18077198