Important: Please read the Qt Code of Conduct -

Adding a TTF font to QML via QFontDatabase fails

  • Hello,

    I am trying to add the Roboto font to my application without having it installed by the operating system. The TTF files were downloaded directly from Google Fonts.

    Working correctly:

    • The TTF files can be correctly installed by Windows and works in QML that way. (QML family/styleName is right)
    • The TTF file is being correctly referenced from my QRC file; QFile(":/fonts/Roboto-Regular.ttf").exists() returns true.

    Not working:

    • QFontDatabase::addApplicationFont(":/fonts/Roboto-Regular.ttf") returns -1.
    • QFontDatabase::addApplicationFontFromData(QFile(":/fonts/Roboto-Regular.ttf").readAll()) returns -1.
    • The font does not appear in my QML.

    Possibly something about the complexity of the TTF file from Google Fonts? Is there any specific metadata missing?


  • When I call:

    qDebug() << QFontDatabase::addApplicationFontFromData(QFile(":/fonts/Roboto-Regular.ttf").readAll());

    The following is output:

    QIODevice::read (QFile, ":/fonts/Roboto-Regular.ttf"): device not open
    QFSFileEngine::open: No file name specified

  • Hey, are you on Linux or something?
    There's a note on the doc:

    Note: Adding application fonts on Unix/X11 platforms without fontconfig is currently not supported.

    So I think you need to install fontconfig on your system first.

    As for the QFile error: it needs to be opened for read before you can perform read on it, just as what it told you.
    But still, this function has the same note as above.

  • I am not on Linux unfortunately; Windows 10.

    I've discovered that explorer.exe has a constant file lock on any .ttf file on my computer instantly. I am assuming that's the cause of the failure to load.

  • I am interested in the solution to this. Can you make the font be part of a qrc and use it from there?

  • @EStudley
    I've tested in my WIndows 10, download the ttf from google fonts and add it to qrc.

    qDebug() << QFontDatabase::addApplicationFont(":/font/Roboto-Regular.ttf");

    this returns 0.
    So at least the code is not the problem.

Log in to reply