Trying to add custom fonts with QFontDatabase::addApplicationFont it returns -1
-
I'm refactoring a desktop application - qt 5.7 vs2013 - which uses custom fonts.
At the moment it loads file "Code New Roman.otf" and it works - addApplicationFont returns 0 and I'm able to visualize fonts correctly.
I've then added some font files in the same directory which is APPLICATIONNAME\fonts then in selected in Project tree fonts directory "Add Existing Files..." and I selected one.
In code I replaced the name of Code New Roman with the new oneint id = QFontDatabase::addApplicationFont(":/fonts/Roboto-Light.ttf");
it always returns -1. I tried with different fonts, otf, ttf... it never works.
Any idea? Thank you
-
Are you sure the font is really in your resource file under :/fonts ? Did you check it with a full path name on your local filesystem instead?
-
I put the font files in the right directory by explorer. Then I added them to app resources by that QtCreator's menu entry. I put it exactly where Code New Roman - the only custom font which works.
I 'm sure about where I put the files in filesystem. I'm not sure QtCreator understand it -
You can pass an absolute path to addApplicationFont().
-
It's for testing if it works in general ...
-
@Christian-Ehrlicher Good call, thank you. If I pass the absolute path it works. weird. I think it depends on how I added this file to qt Creator. I remember I had some issues with the other font as well on one computer and I fixed somehow adding again that file to the project but I'm not sure how
int id = QFontDatabase::addApplicationFont("E:/Sviluppo/AppName/appNameSubDir/fonts/Roboto-Light.ttf"); // itworks
-
Ok, then maybe your resource file path is wrong. You can check it with QFileInfo::exists
-
SOLVED: I previously added fonts by rightclicking on /fonts/fonts folder in Projects panel, then "Add Existing Files...".
This time I added fonts file by rightclicking directly on AppName.qrc, "Add Existing Files...". And files compare under "/" subdirectory called "fonts" not under "/fonts" which is another folder sibling to "/" in hierarchy. Probably old programmers messed up the resource tree structure but qtCreator behaviour about it is not straight or intuitive.
Thank you for assistance.