简体   繁体   中英

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

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.

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)

For all widgets that accept Rich Text you can simply put it into a pre block, ie <pre>This is my Text</pre> . It will then use the systems monospace font.

我在Linux和Windows上使用Qt中的Courier。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM