How to use "QFontDatabase::addApplicationFont"?
-
I want to load a new font from ".ttf" file,so I try to run "QFontDatabase::addApplicationFont".
It doesn't return -1,but I can't load the font.
@
QString loadFontFamilyFromTTF()
{
static QString font;
static bool loaded = false;
if(!loaded)
{
loaded = true;
int loadedFontID = QFontDatabase::addApplicationFont("I:/QtObjects/edit/debug/MSYHMONO.ttf");
QStringList loadedFontFamilies = QFontDatabase::applicationFontFamilies(loadedFontID);
if(!loadedFontFamilies.empty())
font = loadedFontFamilies.at(0);
}
return font;
}
@
@
QString fontFamily = loadFontFamilyFromTTF();
qDebug()<<fontFamily;
QFont font(fontFamily.isEmpty()?QStringLiteral("Courier"):fontFamily,12);
setFont(font);
@
But It doesn't work.
@
"Microsoft YaHei Mono"
QFontEngine::loadEngine: GetTextMetrics failed ()
QWindowsFontEngine: GetTextMetrics failed ()
@
Why?The first qDebug ("qDebug()<<fontFamily;") is true,but QFont can't work with the font family "Microsoft YaHei Mono".
Thank you very much. -
-
[quote author="vidar" date="1359817391"]I had several issues when creating QFonts statically with newer versions of Qt. Does it work when you create the font dynamically?
@QFont * myFont;
QString fontFamily = loadFontFamilyFromTTF();
myFont = new QFont(fontFamily, 8, QFont::Normal, false);@[/quote]
Oh,thank you very much.You are right.