Nominate our 2022 Qt Champions!

Cross platform and font sizes

  • I'd like to hear your take on the issue. I have a fixed size window widget. Textual labels on it.

    If on Mac, the label takes 50% of the width of my available space, in Ubuntu for instance, with the same text and font size, it would take 70% and on Windows, it's 35% This makes the width required for text labels unpredictable from platform to platform.

    How do you deal with something like that?

  • Determine the amount of space allowed, then use QFontMetrics to calculate the appropriate font size.

  • Will give it a try. thanks.

  • Franzk: Maybe I'm just lazy here but how do you use QFontMetrics to calculate the font size for a given width

  • mario: I guess you start by measuring the width of the current font. If it exceeds the width you allocated for it, you begin a loop of testing it with smaller sizes until it fits in your allocated boundaries.

  • Ok, just want to ensure that I don't miss out on some cool features :)

  • Hi,

    I'm interested to this method. Me too experienced the same problem. I used font size in point, to be sure that the size will be proportional and the same under ubuntu and windows, but this don't happens.

    But I have also seen that is not clear how does works the font family specification.

  • Someone once mentioned to me you can actually store the true type font as resource so that the font output is identical through all platforms. never tried it though

  • It's a very interesting feature!!!

    This has also the advantage that the same font can be ported on different platforms (almost desktop). If you have more info about this please tell me. Thanks.

  • I am finishing some other aspects of my code, but I will get to the font question very soon. Font wise, my app has to look identical as much as possible on OSX, Windows and Ubuntu. I have no intention of starting all kind of private cases in my code to support all of them individually. Gee, I hope it won't come to that anyway.

  • "QFontDatabase::addApplicationFont() ": can be your friend.

  • Thank you for the advise. I'm working too on these three platforms and should have the same fonts everywhere.

  • I know the thread is old, but I'd like to recommend for anyone who stumbles on this to use physical values (i.e. pt, in, mm, pc, etc) for your measurements, not pixel values. We can assume that most displays follow the VESA EDID standard, and we can rely on a point (pt) actually being 1/72 of an inch on the screen.

    In Qt C++, you can use the "QScreen": class to get all sorts of info regarding the screen including the dimensions of your screen in mm. In QML, the "Screen": type is available with a similar subset of info. You can use this info to determine what type of device you are on and how your UI should react.

    With this info, you can infer what size the device or viewing area of your app is. With this knowledge, you can decide whether to use measurements relative to the width of the viewing area, or physical values. For example, you might decide to set the base font size of your app to 16pt, or two 10% of the app width, then you can use relative values based on the base font size to render bigger or smaller fonts.

Log in to reply