1,下载获取ttf文件
字体图标下载 - FontAwesome 字体图标中文Icon
2,添加到项目文件
3,项目添加字体库
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QFontDatabase>
#include <QDebug>
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
if(QFontDatabase::addApplicationFont(QLatin1String(":/iconfont.ttf"))==-1)
{
qDebug()<<"load font failed";
}
//查询字体库名称
QFontDatabase qfd;
QStringList qsl = qfd.families();
qDebug()<<qsl;
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();
}
4,Mac查看字体库 Unicode编码
4,qml调用
Text {
font.family : "iconfont"
font.pointSize: 20
text: "\ue889"
anchors.centerIn: parent
color: "white"
}