Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Load font from a file into a QFont object
Is there a way to create a font from a file that is not in the system's font list? For example, if I place a monospace.ttf in the resource system, say :/fonts/monospace.ttf, how can I create a QFont object from this file?
Eddy last edited by
Have a look at "this docnote":http://developer.qt.nokia.com/doc/qt-4.7/qfontdatabase.html#note-47.
lgeyer last edited by
Isn't addApplicationFont() static so it could/should be QFontDatabase::addApplicationFont(...)?
Do you coincidentally know if the note about Unix/X11 and fontconfig is still valid?
I just tried QFontDatabase::addApplicationFont(), with the following findings:
- I was misled a little by the note supplied by Eddy. The name used to built QFont should be the family name of the font. The note used yourfont as both the file name (in the resource file) and the family name (for QFont). But it's pretty easy to figure it out anyway. I used the following code to get the family name without knowing it in advance:
int id = QFontDatabase::addApplicationFont(":/fonts/monospace.ttf");
QString family = QFontDatabase::applicationFontFamilies(id).at(0);
- I still couldn't get my font to work. :-( With Roman character fonts (Courier, Times, Helvecita, etc.) the above method worked flawlessly. But the font I'm trying to use is a Chinese font, and no matter how I tried, I couldn't find a way to get to work. The family name generated by the above code for that font is in Roman characters (containing spaces), which didn't work.
If I install the font into the OS, it is shown with a Chinese family name -- I tried that, and it didn't work either. Maybe it's the encoding problem (although the source said it is a Unicode font, and the system I'm on uses Unicode). I don't know.
Is there a way to check whether Qt really finds the family name or not? AFAIK QFont seems to have some kind of fallback mechanism for characters not in the font family, and even if I give QFont some nonsense family name, the characters would still be shown (with system default?) Is there a way to tell whether QFont really finds a font for the font family I'm giving it?
I just found the answer on a "Chinese page":http://www.bsdmap.com/UNIX_html/XWindow.AIX/00000017/00000003.htm (smth.org is the largest Simplified Chinese BBS in the world).
The problem is how to find the correct family name for QFont. For Roman characters, the name used for internal referring and external displaying are usually the same, but for this is not the case for Chinese fonts (and at least other CJK fonts, I guess). The author uses xlsfonts to get the internal representation of the fonts. For example, in
you have "hanwangcc02" as the family name. Use this in the constructor of QFont, and everything will work.
But my original purpose was to use a font not in the system. I'm still looking for a way to get the font info without loading it into the system -- doesn't fit the topic for this forum.