How to specify monospace fonts for cross platform Qt applications?
For all widgets that accept Rich Text you can simply put it into a pre
block, i.e. <pre>This is my Text</pre>
. It will then use the systems monospace font.
You can retrieve the system's default fixed font using QFontDatabase's systemFont(..) function. It was introduced in Qt 5.2.
Example:
const QFont fixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont)
You can use the style hint property of QFont:
QFont font("Monospace");
font.setStyleHint(QFont::TypeWriter);
If the font cannot be found (which happens with Monospace on Windows), Qt's font matching algorithm tries to find a font that matches the given style hint.