How to specify monospace fonts for cross platform Qt applications?

Is there a platform-independent way to specify a fixed-width font for a Qt widget?

If I set the font to "Monospace" in Designer on Linux, it will not be found on Windows, and Arial will be used instead.

+52
fonts qt
Sep 23 '09 at 19:09
source share
5 answers

You can restore the fixed font to the default using the QFontDatabase systemFont (..) function. It was introduced in Qt 5.2.

Example:

const QFont fixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont) 
+30
Dec 22 '14 at 19:28
source share

You can use the style hint property of the QFont property:

 QFont font("Monospace"); font.setStyleHint(QFont::TypeWriter); 

If the font is not found (what happens with Monospace on Windows), the Qt font matching algorithm tries to find a font that matches the given hint style.

+49
Dec 02 '09 at 21:28
source share

For all widgets that accept Rich Text, you can simply put it in a pre block, i.e. <pre>This is my Text</pre> . Then it will use the system monospace font.

+15
Sep 24 '09 at 14:03
source share

I use Courier in Qt for both Linux and Windows.

+4
Sep 25 '09 at 19:08
source share

I am new, so I can’t comment, but I can confirm that the Courier QtGui.QFont ('courier') sentence above works on MacOS too, while other methods helped me on Linux, but not on Mac

0
May 17 '19 at 2:31
source share



All Articles