Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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)



  • @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");
    

  • Qt Champions 2018

    Or with a FontLoader in 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 between qrc and :


  • Moderators

    @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



  • and urls don't work for the addApplicationFont?


  • Moderators

    @maxwell31
    urls are expected when the parameter type is also QUrl (at least mostly this is the case)
    But if the parameter is of the type QString or named fileName you can assume it expects a file path.



  • Thanks. So here it expects a fileName, and if the file is in the resources, it should start with :/, alright.


Log in to reply