Distributing fonts
-
Hi,
I have an application for which I want to use the material style. The material style uses the Roboto font by google, but it needs to be installed on the system. How can I make sure, that the font is available on a system? Use the Qt-Installer framework and install the fonts (if this is even possible)? Or can I tell Qt to look for the fonts in a certain directory relative to the executable? If I simply put them into my resource file, e.g. the Buttons in Material style don't use the correct font (e.g. fall back to Noto which is installed on my linux machine)
-
Hi,
I have an application for which I want to use the material style. The material style uses the Roboto font by google, but it needs to be installed on the system. How can I make sure, that the font is available on a system? Use the Qt-Installer framework and install the fonts (if this is even possible)? Or can I tell Qt to look for the fonts in a certain directory relative to the executable? If I simply put them into my resource file, e.g. the Buttons in Material style don't use the correct font (e.g. fall back to Noto which is installed on my linux machine)
@maxwell31 You have to add it to the font database. Then you can set the default application font with
QGuiApplication::setFont.QFontDatabase fdb; fdb.addApplicationFont(":/res/fonts/myfont.ttf"); -
Or with a
FontLoaderin QML. -
Adding it to the Font database and using "QGuiApplication::setFont" is working. However, something I wonder: How can I find out the name under which the font is available? Doing this
QFontDatabase::addApplicationFont("qrc:/resources/Roboto-Regular.ttf"); QFontDatabase::addApplicationFont("qrc:/resources/Roboto-Medium.ttf"); QFontDatabase::addApplicationFont("qrc:/resources/Roboto-Light.ttf"); QFontDatabase a; qDebug()<<a.families();does not show the Roboto fonts in the output.
-
Ok, the error was that I have to do
QFontDatabase::addApplicationFont(":/resources/Roboto-Regular.ttf");instead of
qrc:. Actually I don't understand the difference betweenqrcand:@maxwell31 said in Distributing fonts:
instead of qrc:. Actually I don't understand the difference between qrc and :
qrc:/is a url format, wheras:/is a filepath format -
@maxwell31
urls are expected when the parameter type is alsoQUrl(at least mostly this is the case)
But if the parameter is of the typeQStringor namedfileNameyou can assume it expects a file path.