How do you get System default font settings in Qt?
Asked Answered
L

2

20

I am building a desktop app using Qt, my dev machine is win 7 x64 with japanese locale, standard system font is Meiryo. Most of win 7 UI is in this font, though classic/older programs such as ui font customization window itself uses different font which is MS UI Gothic. This doesn't bother me until I found that QtCreator builds my app with MS UI Gothic in one place, and Meiryo in the other. For example, qlabels, qlineedits, qcombobox all uses MS UI Gothic, but a custom completer with a qtableview i add later uses Meiryo.

I made most of the UI in QtCreator's Designer, and the completer I added in code. If I change all widgets' font to Meiryo in The Designer, then of course the app will use Meiryo therefore look right, but in this case I'd rather Qt pick whatever system's default font automatically for me because win 7 will not be the only platform I'll use this program.

What raised my concern even more is that QApplication::font() returns MS UI Gothic, which is false in my case. Of course I can set app-wide font using QApplication::setFont() but that defeats the whole purpose of having the native look-n-feel without micromanaging fonts.

So my question is,

  1. how do Qt determine system default font and,
  2. if this is a Qt Bug how do I go about getting around it?
  3. how should I use .ui files and still have my UI use default system font in runtime?

Some clarifications and facts I found

  1. I want my app to use system default font for EVERY text.

  2. This discussion said that Designer will add font info regardless whether you want it or not. So Qt will honor this info rather than system default font. At least someone mentioned removing this information manually should make Qt pick system default font in runtime.

  3. In my dev machine QApplication::font() returns WRONG default font. BUT how come a QTableView I add later in code uses RIGHT font? Where did it get this info?

So if I find where QTableView finds this info, I can get it in main, and set it app wide with QApplication::setFont(). Then what's left is manually removing all font info, then HOPEFULLY it will work. But this is like why we use Qt in the first place isn't it?

Lesialesion answered 13/6, 2012 at 8:46 Comment(6)
p.s Qt Demo programs showed these symptoms as well, and I don't use binary QtSDK, I compiled Qt myself for each x86 and x64 and use both from QtCreator(the binary distributed one). my toolchain is msvc2010 express.Lesialesion
Unless specified otherwise, Qt uses the default system font to render text. This post briefly discusses the fonts used by default on different operating systems. Perhaps I'm confused, if you just want the default system font per OS, don't do anything and that's what you'll get...If you need to know what that font is for some reason, try this.Malachite
No it seems that this assumption is wrong. See this. Designer will add font information for .ui files, and there is no option to turn it off. What I want is default font REGARDLESS the fact that I use .ui files. And my problem is twofold, because Qt also returns WRONG default font in QApplication::font().Lesialesion
Found this answer which in a totally different context, but applicable nonetheless. I love Qt a lot, but these things should be quickly addressed if they want to keep that 'cross-platform' claimLesialesion
@EliHooten, I tried QFontDatabase, it returns MS UI Gothic, which is wrong.Lesialesion
Based on the previous stack overflow post you linked, it seems like all you can do is the following: "...we have to search through all our .ui files, to make sure that no platform-specific font info has crept in to any .ui file since the previous release." In other words, at release it looks like you have to go through and remove any mention of fonts, which is super frustrating (if it's even possible. I don't use designer so can't comment on it directly). I'm sorry I wasn't more knowledgeable about this at the outset, I tend to shy away from designer and write my Qt code from scratch.Malachite
M
13

I can think of two possible solution:

  1. You could pack with your application a font as a resource file, this way all platforms will use that font regardless the current systems default font.

  2. The QFont class has a method called defaultFamily(). Using this you could manually set the default font for your whole QApplication.

An example (main method):

QApplication application(argc, argv);
QFont font;
font.setFamily(font.defaultFamily());
application.setFont(font);
...rest of your code...
Miserable answered 29/8, 2012 at 13:51 Comment(1)
defaultFamily() on Mac only returns some hard-coded names, rather than what the system itself specifies.Loseff
G
6

Qt will call Windows API SystemParametersInfo to get fonts for QMenu, QMessageBox etc. , and use GetStockObject to get default system font. Some widgets have special font which is different to system default one. I think Qt is doing the right thing, but the default Japanese/Chinese serif font looks bad on HiDPI monitors.

Just use QFont QApplication::font ( const char * className ) to get the right font (Meryo in your case), such as qApp->setFont(QApplication::font("QMenu"))

Gland answered 2/11, 2016 at 13:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.