Adding a TTF font to QML via QFontDatabase fails
-
wrote on 26 Jul 2020, 17:57 last edited by
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?
Thanks.
-
wrote on 26 Jul 2020, 19:35 last edited by
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
-1 -
wrote on 26 Jul 2020, 21:57 last edited by Bonnie
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. -
wrote on 27 Jul 2020, 21:29 last edited by EStudley
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.
-
wrote on 27 Jul 2020, 21:47 last edited by
-
wrote on 27 Jul 2020, 21:49 last edited by
I am interested in the solution to this. Can you make the font be part of a qrc and use it from there?
-
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.
wrote on 27 Jul 2020, 23:32 last edited by Bonnie@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.
1/7