QFontInfo Class▲
-
Header: QFontInfo
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
-
qmake: QT += gui
-
Group: QFontInfo is part of appearance, shared
Detailed Description▲
The QFontInfo class provides the same access functions as QFont, e.g. family(), pointSize(), italic(), weight(), fixedPitch(), styleHint() etc. But whilst the QFont access functions return the values that were set, a QFontInfo object returns the values that apply to the font that will actually be used to draw the text.
For example, when the program asks for a 25pt Courier font on a machine that has a non-scalable 24pt Courier font, QFont will (normally) use the 24pt Courier for rendering. In this case, QFont::pointSize() returns 25 and QFontInfo::pointSize() returns 24.
There are three ways to create a QFontInfo object.
-
Calling the QFontInfo constructor with a QFont creates a font info object for a screen-compatible font, i.e. the font cannot be a printer font. If the font is changed later, the font info object is not updated.
(Note: If you use a printer font the values returned may be inaccurate. Printer fonts are not always accessible so the nearest screen font is used if a printer font is supplied.)
-
QWidget::fontInfo() returns the font info for a widget's font. This is equivalent to calling QFontInfo(widget->font()). If the widget's font is changed later, the font info object is not updated.
-
QPainter::fontInfo() returns the font info for a painter's current font. If the painter's font is changed later, the font info object is not updated.
Checking for the existence of a font▲
Sometimes it can be useful to check if a font exists before attempting to use it. The most thorough way of doing so is by using exactMatch():
const
QFont segoeFont(QLatin1String("Segoe UI"
));
if
(QFontInfo(segoeFont).exactMatch()) {
// Use the font...
}
However, this deep search of families can be expensive on some platforms. QFontDatabase::families().contains() is a faster, but less thorough alternative:
const
QLatin1String segoeUiFamilyName("Segoe UI"
);
if
(QFontDatabase::
families().contains(segoeUiFamilyName)) {
const
QFont segoeFont(segoeUiFamilyName);
// Use the font...
}
It's less thorough because it's not a complete search: some font family aliases may be missing from the list. However, this approach results in faster application startup times, and so should always be preferred if possible.
See Also▲
See also QFont, QFontMetrics, QFontDatabase
Member Function Documentation▲
QFontInfo::QFontInfo(const QFont &font)▲
Constructs a font info object for font.
The font must be screen-compatible, i.e. a font you use when drawing text in widgets or pixmaps, not QPicture or QPrinter.
The font info object holds the information for the font that is passed in the constructor at the time it is created, and is not updated if the font's attributes are changed later.
Use QPainter::fontInfo() to get the font info when painting. This will give correct results also when painting on paint device that is not screen-compatible.
See Also▲
See also Checking for the existence of a font
QFontInfo::QFontInfo(const QFontInfo &fi)▲
Constructs a copy of fi.
QFontInfo::~QFontInfo()▲
Destroys the font info object.
bool QFontInfo::bold() const▲
Returns true if weight() would return a value greater than QFont::Normal; otherwise returns false.
See Also▲
See also weight(), QFont::bold()
bool QFontInfo::exactMatch() const▲
Returns true if the matched window system font is exactly the same as the one specified by the font; otherwise returns false.
See Also▲
See also QFont::exactMatch()
QString QFontInfo::family() const▲
Returns the family name of the matched window system font.
See Also▲
See also QFont::family(), Checking for the existence of a font
bool QFontInfo::fixedPitch() const▲
Returns the fixed pitch value of the matched window system font.
See Also▲
See also QFont::fixedPitch()
bool QFontInfo::italic() const▲
int QFontInfo::pixelSize() const▲
int QFontInfo::pointSize() const▲
Returns the point size of the matched window system font.
See Also▲
See also pointSizeF(), QFont::pointSize()
qreal QFontInfo::pointSizeF() const▲
QFont::Style QFontInfo::style() const▲
QFont::StyleHint QFontInfo::styleHint() const▲
Returns the style of the matched window system font.
Currently only returns the style hint set in QFont.
See Also▲
See also QFont::styleHint(), QFont::StyleHint
QString QFontInfo::styleName() const▲
Returns the style name of the matched window system font on systems that support it.
See Also▲
See also QFont::styleName()
void QFontInfo::swap(QFontInfo &other)▲
Swaps this font info instance with other. This function is very fast and never fails.
int QFontInfo::weight() const▲
QFontInfo &QFontInfo::operator=(const QFontInfo &fi)▲
Assigns the font info in fi.
Obsolete Members for QFontInfo▲
The following members of class QFontInfo are deprecated. We strongly advise against using them in new code.
Obsolete Member Function Documentation▲
int QFontInfo::legacyWeight() const▲
This function is deprecated. We strongly advise against using it in new code.
Use weight() instead.
Returns the weight of the font converted to the non-standard font weight scale used in Qt 5 and earlier versions.
Since Qt 6, the OpenType standard's font weight scale is used instead of a non-standard scale. This requires conversion from values that use the old scale. For convenience, this function may be used when porting from code which uses the old weight scale.
See Also▲
See also QFont::setWeight(), weight(), QFontInfo