Is it possible to apply a custom font to all elements in QML?

若如初见. 提交于 2020-07-23 11:06:50

问题


Like the question says, i want to know if there is a way to apply a custom font to all elements in QML like we can easily do in HTML setting the font on the tag body. I already have the font accessible in QML with:

FontLoader { id: robotoRegular; source: "fonts/Roboto-Regular.ttf" }

But now if i do something like

ApplicationWindow {
font.family:robotoRegular.name

It doesn't seem to work, and probably isnt supposed to. Or is there a way?

Im using Qt 5.11.2, QtQuick 2.2


回答1:


Try this code:

#include <QFontDatabase>

void InstallDefaultFont()
{
    qint32 fontId = QFontDatabase::addApplicationFont(":/font.otf");
    QStringList fontList = QFontDatabase::applicationFontFamilies(fontId);

    QString family = fontList.at(0);
    QGuiApplication::setFont(QFont(family));
}

This will setup the default font for QML and Widgets as well.



来源:https://stackoverflow.com/questions/53496266/is-it-possible-to-apply-a-custom-font-to-all-elements-in-qml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!