QFontMetrics Class
The QFontMetrics class provides font metrics information. More…
| Header | #include <QFontMetrics> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Gui) |
| Module | Nt GUI C++ Classes |
List of all members, including inherited members
Public Functions
| QFontMetrics(const QFont &font) | |
| QFontMetrics(const QFontMetrics &other) | |
| QFontMetrics & | operator=(const QFontMetrics &other) |
| ~QFontMetrics() | |
| int | ascent() const |
| int | descent() const |
| int | height() const |
| int | leading() const |
| int | lineSpacing() const |
| int | xHeight() const |
| int | averageCharWidth() const |
| bool | inFont(QChar ch) const |
| int | horizontalAdvance(const QString &text, int length = -1) const |
| int | horizontalAdvance(QChar ch) const |
| QRect | boundingRect(const QString &text) const |
| QRect | boundingRect(const QRect &rect, int flags, const QString &text) const |
| QRect | boundingRect(int x, int y, int w, int h, int flags, const QString &text) const |
| QSize | size(int flags, const QString &text) const |
| QString | elidedText(const QString &text, Qt::TextElideMode mode, int width, int flags = 0) const |
Detailed Description
Font metrics measure text in the pixels of a font: the height of a line and the width of a string. Use them to lay text out:
QFontMetrics fm(QFont("Noto Sans", 12));
int width = fm.horizontalAdvance("Hello");
QString shorter = fm.elidedText("A very long file name.txt", Qt::ElideMiddle, 120);
See also QFont and QPainter::drawText().
Differences from Qt
There is no QFontMetricsF, tightBoundingRect(), leftBearing(), maxWidth(), underlinePos() or capHeight(). Text is measured as a run of characters without shaping or kerning, so complex scripts such as Arabic are measured wrongly. The metrics are those of the font at 96 dots per inch, not of a paint device.
Member Function Documentation
QFontMetrics::QFontMetrics(const QFont &font)
Constructs font metrics for font.
QFontMetrics::QFontMetrics(const QFontMetrics &other)
Constructs a copy of other.
QFontMetrics::~QFontMetrics()
Destroys the metrics.
int QFontMetrics::ascent() const
Returns the distance from the baseline to the top of the tallest character, in pixels.
See also descent() and height().
int QFontMetrics::averageCharWidth() const
Returns the width of a digit, which is a fair guess at the average width of a character.
QRect QFontMetrics::boundingRect(const QString &text) const
Returns the rectangle that text covers when it is drawn at the origin with QPainter::drawText(QPoint, QString): its top is at minus the ascent.
See also size().
QRect QFontMetrics::boundingRect(const QRect &rect, int flags, const QString &text) const
This is an overloaded function.
Returns the rectangle that text covers when it is drawn with QPainter::drawText() in rect with the given flags, which are Qt::AlignmentFlag and Qt::TextFlag values.
QRect QFontMetrics::boundingRect(int x, int y, int w, int h, int flags, const QString &text) const
This is an overloaded function.
Returns the rectangle that text covers when it is drawn in the rectangle at (x, y) of w by h with the given flags.
int QFontMetrics::descent() const
Returns the distance from the baseline to the bottom of the lowest character, in pixels.
See also ascent() and height().
QString QFontMetrics::elidedText(const QString &text, Qt::TextElideMode mode, int width, int flags = 0) const
Returns text shortened to fit in width pixels, with an ellipsis where mode says. Text that fits, or Qt::ElideNone, is returned as it is. The flags are accepted for compatibility; only the first line is considered with Qt::TextSingleLine.
int QFontMetrics::height() const
Returns the height of a line of text: the ascent plus the descent.
See also lineSpacing().
int QFontMetrics::horizontalAdvance(const QString &text, int length = -1) const
Returns the width in pixels of the first length characters of text, or of all of it if length is negative, drawn on one line.
See also boundingRect().
int QFontMetrics::horizontalAdvance(QChar ch) const
This is an overloaded function.
Returns the width in pixels of the character ch.
bool QFontMetrics::inFont(QChar ch) const
Returns true if the font has a glyph for ch.
int QFontMetrics::leading() const
Returns the gap between the descent of one line and the ascent of the next.
See also lineSpacing().
int QFontMetrics::lineSpacing() const
Returns the distance between the baselines of two lines of text: the height plus the leading.
See also height() and leading().
QFontMetrics & QFontMetrics::operator=(const QFontMetrics &other)
Assigns other to these metrics.
QSize QFontMetrics::size(int flags, const QString &text) const
Returns the size of text when it is drawn with the given flags. With Qt::TextWordWrap the text has no width limit, so only new lines break it.
See also boundingRect().
int QFontMetrics::xHeight() const
Returns the height of the lower case letter x, in pixels.