简体   繁体   English

如何为跨平台Qt应用程序指定等宽字体?

[英]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 ? 是否有一种独立于平台的方式来为Qt小部件指定固定宽度的字体?

If I set the font to "Monospace" in Designer on Linux, it is not found on Windows and Arial is used instead. 如果我在Linux上的Designer中将字体设置为“Monospace”,则在Windows上找不到它,而是使用Arial。

You can use the style hint property of QFont: 您可以使用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. 如果找不到该字体(在Windows上使用Monospace时会发生这种情况),Qt的字体匹配算法会尝试查找与给定样式提示相匹配的字体。

You can retrieve the system's default fixed font using QFontDatabase's systemFont(..) function. 您可以使用QFontDatabase的systemFont(..)函数检索系统的默认固定字体。 It was introduced in Qt 5.2. 它在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> . 对于所有接受Rich Text的小部件,您只需将其放入pre阻止,即<pre>This is my Text</pre> It will then use the systems monospace font. 然后它将使用系统monospace字体。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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