问题
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