本页内容

QFontInfo Class

QFontInfo 类提供了有关字体的通用信息。更多内容...

头文件: #include <QFontInfo>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui

注意:该类中的所有函数均为可重入函数。

公共函数

QFontInfo(const QFont &font)
QFontInfo(const QFontInfo &fi)
QFontInfo(QFontInfo &&)
~QFontInfo()
bool bold() const
bool exactMatch() const
QString family() const
bool fixedPitch() const
bool italic() const
int pixelSize() const
int pointSize() const
qreal pointSizeF() const
QFont::Style style() const
QFont::StyleHint styleHint() const
QString styleName() const
void swap(QFontInfo &other)
(since 6.9) QList<QFontVariableAxis> variableAxes() const
int weight() const
QFontInfo &operator=(const QFontInfo &fi)

详细说明

QFontInfo 类提供了与QFont 相同的访问函数,例如family()、pointSize()、italic()、weight()、fixedPitch()、styleHint() 等。但QFont 的访问函数返回的是已设置的值,而 QFontInfo 对象返回的是实际用于绘制文本的字体的值。

例如,当程序在仅拥有不可缩放的 24pt Courier 字体的机器上请求 25pt Courier 字体时,QFont (通常)会使用 24pt Courier 进行渲染。在此情况下,QFont::pointSize() 返回 25,而QFontInfo::pointSize() 返回 24。

创建 QFontInfo 对象有三种方法。

  1. 通过传入QFont 调用 QFontInfo 构造函数,将创建一个屏幕兼容字体的字体信息对象,即该字体不能是打印机字体。如果后续更改了字体,字体信息对象不会随之更新。

    (注意:如果使用打印机字体,返回的值可能不准确。由于打印机字体并非总是可用的,因此如果提供了打印机字体,系统会使用最接近的屏幕字体。)

  2. QWidget::fontInfo() 返回小部件字体的字体信息。这等同于调用 QFontInfo(widget->font())。如果后续更改了小部件的字体,字体信息对象不会被更新。
  3. QPainter::fontInfo() 返回绘图器当前字体的字体信息。如果后续更改了绘图器的字体,字体信息对象将不会更新。

检查字体是否存在

有时在尝试使用字体之前,检查该字体是否存在会很有帮助。最彻底的方法是使用 `exactMatch()`:

const QFont segoeFont(QLatin1String("Segoe UI"));
if (QFontInfo(segoeFont).exactMatch()) {
    // Use the font...
}

然而,在某些平台上,这种对字体家族的深度搜索可能消耗较大资源。QFontDatabase::families().contains() 是一种更快但不够彻底的替代方案:

const QLatin1String segoeUiFamilyName("Segoe UI");
if (QFontDatabase::families().contains(segoeUiFamilyName)) {
    const QFont segoeFont(segoeUiFamilyName);
    // Use the font...
}

其全面性较低,因为这不是一次完整的搜索:列表中可能会遗漏某些字体家族的别名。不过,这种方法能缩短应用程序的启动时间,因此在可能的情况下应优先采用。

另请参阅 QFont 、QFontMetrics 以及QFontDatabase 。

成员函数文档

QFontInfo::QFontInfo(const QFont &font)

为font 构建一个字体信息对象。

该字体必须是屏幕兼容的,即在widgets 或pixmaps 中绘制文本时使用的字体,而非QPicture 或QPrinter 。

字体信息对象保存着创建时通过构造函数传入的字体信息,如果后续修改了字体的属性,该对象不会更新。

在绘制时,请使用 `QPainter::fontInfo()` 获取字体信息。即使在非屏幕兼容的绘制设备上绘制,此方法也能返回正确的结果。

另请参阅 Checking for the existence of a font 。

QFontInfo::QFontInfo(const QFontInfo &fi)

创建fi 的副本。

[constexpr noexcept default] QFontInfo::QFontInfo(QFontInfo &&)

通过Move构造一个QFontInfo 实例。

[noexcept] QFontInfo::~QFontInfo()

销毁字体信息对象。

bool QFontInfo::bold() const

如果weight() 返回的值大于QFont::Normal ,则返回true ;否则返回false 。

另请参阅 weight() 和QFont::bold()。

bool QFontInfo::exactMatch() const

如果匹配到的窗口系统字体与 font 指定的字体完全相同,则返回true ;否则返回false 。

另请参阅 QFont::exactMatch()。

QString QFontInfo::family() const

返回匹配的窗口系统字体的字体家族名称。

另请参阅 QFont::family() 和Checking for the existence of a font 。

bool QFontInfo::fixedPitch() const

返回匹配窗口系统字体的固定字距值。

另请参阅 QFont::fixedPitch()。

bool QFontInfo::italic() const

返回匹配的窗口系统字体的斜体值。

另请参阅 QFont::italic()。

int QFontInfo::pixelSize() const

返回匹配的窗口系统字体的像素大小。

另请参阅 QFont::pointSize()。

int QFontInfo::pointSize() const

返回匹配的窗口系统字体的点大小。

另请参阅 pointSizeF() 和QFont::pointSize()。

qreal QFontInfo::pointSizeF() const

返回匹配的窗口系统字体的点大小。

另请参阅 QFont::pointSizeF()。

QFont::Style QFontInfo::style() const

返回匹配的窗口系统字体的样式值。

另请参阅 QFont::style()。

QFont::StyleHint QFontInfo::styleHint() const

返回匹配的窗口系统字体的样式。

目前仅返回在QFont 中设置的样式提示。

另请参阅 QFont::styleHint() 和QFont::StyleHint 。

QString QFontInfo::styleName() const

在支持该功能的系统上,返回匹配的窗口系统字体的样式名称。

另请参阅 QFont::styleName()。

[noexcept] void QFontInfo::swap(QFontInfo &other)

将此字体信息实例替换为other 。此操作非常快速,且绝不会失败。

[since 6.9] QList<QFontVariableAxis> QFontInfo::variableAxes() const

如果该字体是可变字体,则此函数将返回该字体支持的轴列表。

有关可变坐标轴的更多详细信息,请参阅setVariableAxis()。

该函数于 Qt 6.9 中引入。

int QFontInfo::weight() const

返回匹配的窗口系统字体的字重。

另请参阅 QFont::weight() 和bold()。

QFontInfo &QFontInfo::operator=(const QFontInfo &fi)

将fi 中的字体信息赋值给该变量。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.