You can embed a True Type font or Open Type font into your Qt application using QFontDatabase. You can link to an external font file or link to a font embedded as a resource.
First, make sure you include a reference to the QFontDatabase class:
#include <QFontDatabase>;
To link to an external font, do this:
QFontDatabase database; int result = database.addApplicationFont("/path/to/font.tff");
Or for a more reliable way, embed the font as a resource. Create a Qt resource file and add a reference to the font file you want embedded. Then use the path to the embedded resource font:
QFontDatabase database; int result = database.addApplicationFont(":/resource/path/to/font.tff");
If the returned integer is greater than -1, then the font has successfully loaded into the font database. It’s essentially storing the font in memory at the application’s runtime. You can check your operating system’s installed fonts and if the font hasn’t already been installed, that using QFontDatabase doesn’t install the font on your system. So this is a cool way of having specific fonts just for your Qt app without messing with the end-user’s fonts.
And to use the font, you would call it from the font database:
QFont f = database.font("myFont", "normal", 12); myWidget.setFont(f);
Of course, be mindful of the font you choose for your app. As graphic designers, we are taught that for digital screens, you want to use sans-serif fonts (vs serif fonts for print). There needs to be good whitespace. Since users will be staring at the fonts for long periods of time, it needs to look nice and balanced overall. Then there’s also the issue of visual consistency and compatibility across operating systems. In this case, you don’t have to embed a custom font, but make sure you choose a web-safe font.