Custom font for whole application
-
Hi, I want to apply custom font to whole application.
So, I imported font files(.otf) into .qrc<RCC> <qresource prefix="/fonts"> <file>res/SCDream1.otf</file> <file>res/SCDream2.otf</file> <file>res/SCDream3.otf</file> <file>res/SCDream4.otf</file> <file>res/SCDream5.otf</file> <file>res/SCDream6.otf</file> <file>res/SCDream7.otf</file> <file>res/SCDream8.otf</file> <file>res/SCDream9.otf</file> </qresource> </RCC>
And call QFontDatabase::addApplicationFont() in main
int main(int argc, char *argv[]) { QApplication a(argc, argv); // load font int n = -1; for (int i = 1; i < 10; ++i) { n = QFontDatabase::addApplicationFont(QString(":/fonts/res/SCDream%1.otf").arg(i)); // (1) qDebug() << QFontDatabase::applicationFontFamilies(n); // (2) } // set application's default font QFont defaultFont; defaultFont.setFamily("S-Core Dream"); defaultFont.setPointSize(18); a.setFont(defaultFont); qDebug() << a.font().family(); // (3) // ... myApp w; w.show(); return a.exec(); }
In (1), return value of addApplicationFont() is 0 to 8. it's fine.
In (2), debug message is("S-Core Dream 1 Thin") ("S-Core Dream 2 ExtraLight") ("S-Core Dream 3 Light") ("S-Core Dream 4 Regular") ("S-Core Dream 5 Medium") ("S-Core Dream 6 Bold") ("S-Core Dream 7 ExtraBold") ("S-Core Dream 8 Heavy") ("S-Core Dream 9 Black")
and this is also fine.
In (3), debug message for the font set in the application is"S-Core Dream"
and that's fine too. (maybe not?)
But application fonts are not changed at all.
In each UI, except for the type of font, the size and weight are set using a setStyleSheet like :// child widget's constructor setStyleSheet("pushButton {font-size: 50px; font-weight: 600;}"); // The font-size is applied, but the weight is not applied. I think it's because it's a default font (which doesn't support weights), not a custom font type.
At first, there was no problem in my PC, but when I ran it on another Windows 10 PC for testing, the font was applied or not depending on the PC, and it seems to be affected by whether the font is installed on the PC, but I do not understand . The font was definitely included in the .qrc and built (size of exe is increases), and the debugging message appeared the same (all load and set is fine) even on the PC where the font was not applied.
I tried calling by specifying the path without adding each font file to .qrc, but it didn't work.
QFontDatabase::addApplicationFont(QString(a.applicationDirPath() + "/fonts/SCDream%1.otf").arg(i));
What am I missing?
I am developing with Qt Visual Studio addOn(Qt VS Tools) with VS2017 on Windows 10 21H2, Qt version is 5.14.1.
-
Each of your font files provides an independent font family, as shown in your debug output. Note that none of these families is "S-Core Dream", so a substitution may be occurring. From the docs, " If the family isn't available a family will be set using the font matching algorithm."
You could pass defaultFont to QFontInfo to see what is actually selected.
What happens if you set defaultFont with family "S-Core Dream 4 Regular"?