Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify monospace fonts for cross platform Qt applications?

Tags:

fonts

qt

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 is not found on Windows and Arial is used instead.

like image 842
Luper Rouch Avatar asked Sep 23 '09 19:09

Luper Rouch


People also ask

Where do you put the monospace font?

Monospaced fonts are customary on typewriters and for typesetting computer code. Monospaced fonts were widely used in early computers and computer terminals, which often had extremely limited graphical capabilities.

Are monospaced fonts accessible?

Monospaced typefaces do reduce legibility, albeit by a margin. In Universal Principles of Design, the entry on legibility states: Proportionally spaced typefaces are preferred over monospaced.

How do I make my font monospaced?

Basically, for a font to be considered monospace, every glyph has to be the same width, right down to the exact same number of units. This includes even glyphs that should normally be zero width, or a certain width (such as em spaces, em dashes, etc).


1 Answers

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.

like image 198
Torsten Marek Avatar answered Sep 23 '22 15:09

Torsten Marek