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()
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() 返回 widget 字体的字体信息。这等同于调用 QFontInfo(widget->font()) 。如果以后更改了 widget 的字体,则不会更新字体信息对象。
  3. QPainter::fontInfoQFontInfo() 返回画家当前字体的字体信息。如果以后更改了绘画者的字体,则不会更新字体信息对象。

检查字体是否存在

有时,在尝试使用字体之前,检查字体是否存在会很有用。最彻底的方法是使用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 构建字体信息对象。

字体必须与屏幕兼容,即在widgetspixmaps 中绘制文本时使用的字体,而不是QPictureQPrinter

字体信息对象保存的是创建字体时在构造函数中传递的字体信息,如果以后更改了字体属性,该信息也不会更新。

在绘制时,使用QPainter::fontInfo() 获取字体信息。在不兼容屏幕的设备上绘制时,也能获得正确的结果。

另请参见 Checking for the existence of a font

QFontInfo::QFontInfo(const QFontInfo &fi)

构造fi 的副本。

[noexcept] QFontInfo::~QFontInfo()

销毁字体信息对象。

bool QFontInfo::bold() const

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

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

bool QFontInfo::exactMatch() const

如果匹配的窗口系统字体与字体指定的字体完全相同,则返回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 中指定字体信息。

© 2025 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.