Multiple OSs, one codebase, Part 2 - Fonts

September 30, 2026 Qt Software

Now the pain points in this one are entirely the fault of the OSs. Each seems to have their own way to manage and display fonts, and never the four shall meet.

Qt handles this admirably and if you are just creating a straight widgets app, you will never need to worry about fonts. Qt will take care of you.

However, if you are trying to duplicate a character sheet on an image of said sheet, the suddenly exactly what a font looks like and how much room the text takes becomes vitally important. Here is where you find that just because you asked for a 12-point font, it may not use the font you tell it to, and it probably won't be the exact same size between the different OSs.

This where two things Qt tells you about fonts becomes vitally important:

int width = font.horizontalAdvance("<some string>);

and

int height - font.height();

If you know anything about fonts, you might be holding up your hand and asking 'but how do you know height id the right method?' And the answer is because I read the documentation and height is guaranteed to be the ascent plus the descent. If you don't know fonts, just trust me and use height.

Now, for the icky part (at least for me. I dislike having to use #IFs in my code, but here we are).

I should have set this up in an include file, but I was not aware of the tangle in front of me at the time. Learn from my mistakes!

#pragma once

// For each OS we need to set up a series of sizes, and sometimes character widths
// of any styles we are going to use. Italic, Bold, Underline - don't worry. Point
// size, however, is the single most vital thing here.

#if defined(Q_OS_ANDROID)
static constexpr auto BigFontSize    = 24;
static constexpr auto MediumFontSize = 18;
static constexpr auto SmallFontSize  = 12;
static constexpr auto Offset         = 1;
#elif defined(__wasm__)
// versions for the web
static constexpr auto Offset         = 2;
#elif defined(UNIX)
// version for linux    (why unix, Qt, why? You really only support linux!)
static constexpr auto Offset         = -1;
#elif defined(MSVC)              // not a blank else because someday, maybe, a MAC
// versions for Windows PCs
static constexpr auto Offset         = 0;
#endif

Here's the fun part. This is all discovered through a lot of trial and error. A lot of it. And squinting at the screen trying to make sense of why it's so broken in this particular OS. Trust me on that. The offset is important (and it all depends on so many variables (espeecially the font) that I just winged it up there, do not take them as gospel). When having something at exactly the right pixel matters, offset lets you get all of the OSs on the same visual place. At least it's a constant.

You use the width of some text to help you with centering, normally. It's not useful for much else, unless you need to make sure the next text item isn't too far away from each another or not on top of each other. Ask me how I know this ;-)

Comments (0)

No comments yet. Be the first hero to speak up.

Leave a comment