
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQFontMetricsF class provides font metrics information.More...
| Header: | #include <QFontMetricsF> |
Note: All functions in this class arereentrant.
| QFontMetricsF(const QFont & font) | |
| QFontMetricsF(const QFont & font, QPaintDevice * paintdevice) | |
| QFontMetricsF(const QFontMetrics & fontMetrics) | |
| QFontMetricsF(const QFontMetricsF & fm) | |
| ~QFontMetricsF() | |
| qreal | ascent() const |
| qreal | averageCharWidth() const |
| QRectF | boundingRect(const QString & text) const |
| QRectF | boundingRect(QChar ch) const |
| QRectF | boundingRect(const QRectF & rect, int flags, const QString & text, int tabStops = 0, int * tabArray = 0) const |
| qreal | descent() const |
| QString | elidedText(const QString & text, Qt::TextElideMode mode, qreal width, int flags = 0) const |
| qreal | height() const |
| bool | inFont(QChar ch) const |
| bool | inFontUcs4(uint ch) const |
| qreal | leading() const |
| qreal | leftBearing(QChar ch) const |
| qreal | lineSpacing() const |
| qreal | lineWidth() const |
| qreal | maxWidth() const |
| qreal | minLeftBearing() const |
| qreal | minRightBearing() const |
| qreal | overlinePos() const |
| qreal | rightBearing(QChar ch) const |
| QSizeF | size(int flags, const QString & text, int tabStops = 0, int * tabArray = 0) const |
| qreal | strikeOutPos() const |
| QRectF | tightBoundingRect(const QString & text) const |
| qreal | underlinePos() const |
| qreal | width(const QString & text) const |
| qreal | width(QChar ch) const |
| qreal | xHeight() const |
| bool | operator!=(const QFontMetricsF & other) |
| bool | operator!=(const QFontMetricsF & other) const |
| QFontMetricsF & | operator=(const QFontMetricsF & fm) |
| QFontMetricsF & | operator=(const QFontMetrics & other) |
| QFontMetricsF & | operator=(QFontMetricsF && other) |
| bool | operator==(const QFontMetricsF & other) |
| bool | operator==(const QFontMetricsF & other) const |
TheQFontMetricsF class provides font metrics information.
QFontMetricsF functions calculate the size of characters and strings for a given font. You can construct aQFontMetricsF object with an existingQFont to obtain metrics for that font. If the font is changed later, the font metrics object isnot updated.
Once created, the object provides functions to access the individual metrics of the font, its characters, and for strings rendered in the font.
There are several functions that operate on the font:ascent(),descent(),height(),leading() andlineSpacing() return the basic size properties of the font. TheunderlinePos(),overlinePos(),strikeOutPos() andlineWidth() functions, return the properties of the line that underlines, overlines or strikes out the characters. These functions are all fast.
There are also some functions that operate on the set of glyphs in the font:minLeftBearing(),minRightBearing() andmaxWidth(). These are by necessity slow, and we recommend avoiding them if possible.
For each character, you can get itswidth(),leftBearing() andrightBearing() and find out whether it is in the font usinginFont(). You can also treat the character as a string, and use the string functions on it.
The string functions includewidth(), to return the width of a string in pixels (or points, for a printer),boundingRect(), to return a rectangle large enough to contain the rendered string, andsize(), to return the size of that rectangle.
Example:
See alsoQFont,QFontInfo, andQFontDatabase.
Constructs a font metrics object forfont.
The font metrics will be compatible with the paintdevice used to createfont.
The font metrics 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.
UseQFontMetricsF(constQFont &,QPaintDevice *) to get the font metrics that are compatible with a certain paint device.
Constructs a font metrics object forfont andpaintdevice.
The font metrics will be compatible with the paintdevice passed. If thepaintdevice is 0, the metrics will be screen-compatible, ie. the metrics you get if you use the font for drawing text on awidgets orpixmaps, not on aQPicture orQPrinter.
The font metrics 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.
Constructs a font metrics object with floating point precision from the givenfontMetrics object.
This function was introduced in Qt 4.2.
Constructs a copy offm.
Destroys the font metrics object and frees all allocated resources.
Returns the ascent of the font.
The ascent of a font is the distance from the baseline to the highest position characters extend to. In practice, some font designers break this rule, e.g. when they put more than one accent on top of a character, or to accommodate an unusual character in an exotic language, so it is possible (though rare) that this value will be too small.
See alsodescent().
Returns the average width of glyphs in the font.
This function was introduced in Qt 4.2.
Returns the bounding rectangle of the characters in the string specified bytext. The bounding rectangle always covers at least the set of pixels the text would cover if drawn at (0, 0).
Note that the bounding rectangle may extend to the left of (0, 0), e.g. for italicized fonts, and that the width of the returned rectangle might be different than what thewidth() method returns.
If you want to know the advance width of the string (to layout a set of strings next to each other), usewidth() instead.
Newline characters are processed as normal characters,not as linebreaks.
The height of the bounding rectangle is at least as large as the value returnedheight().
See alsowidth(),height(), andQPainter::boundingRect().
Returns the bounding rectangle of the characterch relative to the left-most point on the base line.
Note that the bounding rectangle may extend to the left of (0, 0), e.g. for italicized fonts, and that the text output may coverall pixels in the bounding rectangle.
Note that the rectangle usually extends both above and below the base line.
See alsowidth().
This is an overloaded function.
Returns the bounding rectangle of the characters in the giventext. This is the set of pixels the text would cover if drawn when constrained to the bounding rectangle specified byrect.
Theflags argument is the bitwise OR of the following flags:
Qt::AlignHCenter | Qt::AlignVCenter)Qt::Horizontal alignment defaults toQt::AlignLeft and vertical alignment defaults toQt::AlignTop.
If several of the horizontal or several of the vertical alignment flags are set, the resulting alignment is undefined.
These flags are defined inQt::AlignmentFlag.
IfQt::TextExpandTabs is set inflags, the following behavior is used to interpret tab characters in the text:
Note that the bounding rectangle may extend to the left of (0, 0), e.g. for italicized fonts.
Newline characters are processed as line breaks.
Despite the different actual character heights, the heights of the bounding rectangles of "Yes" and "yes" are the same.
The bounding rectangle returned by this function is somewhat larger than that calculated by the simplerboundingRect() function. This function uses themaximum left andright font bearings as is necessary for multi-line text to align correctly. Also, fontHeight() andlineSpacing() are used to calculate the height, rather than individual character heights.
See alsowidth(),QPainter::boundingRect(), andQt::Alignment.
Returns the descent of the font.
The descent is the distance from the base line to the lowest point characters extend to. (Note that this is different from X, which adds 1 pixel.) In practice, some font designers break this rule, e.g. to accommodate an unusual character in an exotic language, so it is possible (though rare) that this value will be too small.
See alsoascent().
If the stringtext is wider thanwidth, returns an elided version of the string (i.e., a string with "..." in it). Otherwise, returns the original string.
Themode parameter specifies whether the text is elided on the left (e.g., "...tech"), in the middle (e.g., "Tr...ch"), or on the right (e.g., "Trol...").
Thewidth is specified in pixels, not characters.
Theflags argument is optional and currently only supportsQt::TextShowMnemonic as value.
This function was introduced in Qt 4.2.
Returns the height of the font.
This is always equal toascent()+descent()+1 (the 1 is for the base line).
See alsoleading() andlineSpacing().
Returns true if characterch is a valid character in the font; otherwise returns false.
Returns true if the character given bych, encoded in UCS-4/UTF-32, is a valid character in the font; otherwise returns false.
This function was introduced in Qt 4.8.
Returns the leading of the font.
This is the natural inter-line spacing.
See alsoheight() andlineSpacing().
Returns the left bearing of characterch in the font.
The left bearing is the right-ward distance of the left-most pixel of the character from the logical origin of the character. This value is negative if the pixels of the character extend to the left of the logical origin.
See width(QChar) for a graphical description of this metric.
See alsorightBearing(),minLeftBearing(), andwidth().
Returns the distance from one base line to the next.
This value is always equal toleading()+height().
See alsoheight() andleading().
Returns the width of the underline and strikeout lines, adjusted for the point size of the font.
See alsounderlinePos(),overlinePos(), andstrikeOutPos().
Returns the width of the widest character in the font.
Returns the minimum left bearing of the font.
This is the smallestleftBearing(char) of all characters in the font.
Note that this function can be very slow if the font is large.
See alsominRightBearing() andleftBearing().
Returns the minimum right bearing of the font.
This is the smallestrightBearing(char) of all characters in the font.
Note that this function can be very slow if the font is large.
See alsominLeftBearing() andrightBearing().
Returns the distance from the base line to where an overline should be drawn.
See alsounderlinePos(),strikeOutPos(), andlineWidth().
Returns the right bearing of characterch in the font.
The right bearing is the left-ward distance of the right-most pixel of the character from the logical origin of a subsequent character. This value is negative if the pixels of the character extend to the right of thewidth() of the character.
Seewidth() for a graphical description of this metric.
See alsoleftBearing(),minRightBearing(), andwidth().
Returns the size in pixels of the characters in the giventext.
Theflags argument is the bitwise OR of the following flags:
These flags are defined inQt::TextFlags.
IfQt::TextExpandTabs is set inflags, the following behavior is used to interpret tab characters in the text:
Newline characters are processed as line breaks.
Note: Despite the different actual character heights, the heights of the bounding rectangles of "Yes" and "yes" are the same.
See alsoboundingRect().
Returns the distance from the base line to where the strikeout line should be drawn.
See alsounderlinePos(),overlinePos(), andlineWidth().
Returns a tight bounding rectangle around the characters in the string specified bytext. The bounding rectangle always covers at least the set of pixels the text would cover if drawn at (0, 0).
Note that the bounding rectangle may extend to the left of (0, 0), e.g. for italicized fonts, and that the width of the returned rectangle might be different than what thewidth() method returns.
If you want to know the advance width of the string (to layout a set of strings next to each other), usewidth() instead.
Newline characters are processed as normal characters,not as linebreaks.
Warning: Calling this method is very slow on Windows.
This function was introduced in Qt 4.3.
See alsowidth(),height(), andboundingRect().
Returns the distance from the base line to where an underscore should be drawn.
See alsooverlinePos(),strikeOutPos(), andlineWidth().
Returns the width in pixels of the characters in the giventext.
Note that this value isnot equal to the width returned byboundingRect().width() becauseboundingRect() returns a rectangle describing the pixels this string will cover whereas width() returns the distance to where the next string should be drawn.
See alsoboundingRect().
This is an overloaded function.

Returns the logical width of characterch in pixels. This is a distance appropriate for drawing a subsequent character afterch.
Some of the metrics are described in the image to the right. The central dark rectangles cover the logicalwidth() of each character. The outer pale rectangles cover theleftBearing() andrightBearing() of each character. Notice that the bearings of "f" in this particular font are both negative, while the bearings of "o" are both positive.
Warning: This function will produce incorrect results for Arabic characters or non-spacing marks in the middle of a string, as the glyph shaping and positioning of marks that happens when processing strings cannot be taken into account. When implementing an interactive text control, useQTextLayout instead.
See alsoboundingRect().
Returns the 'x' height of the font. This is often but not always the same as the height of the character 'x'.
Returns true if the font metrics are not equal to theother font metrics; otherwise returns false.
See alsooperator==().
This is an overloaded function.
Returns true if the font metrics are not equal to theother font metrics; otherwise returns false.
See alsooperator==().
Assigns the font metricsfm to this font metrics object.
Assignsother to this object.
This function was introduced in Qt 4.2.
Returns true if the font metrics are equal to theother font metrics; otherwise returns false.
Two font metrics are considered equal if they were constructed from the sameQFont and the paint devices they were constructed for are considered to be compatible.
This is an overloaded function.
Returns true if the font metrics are equal to theother font metrics; otherwise returns false.
Two font metrics are considered equal if they were constructed from the sameQFont and the paint devices they were constructed for are considered to be compatible.
© 2016 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 theGNU 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.