HomeHome

Classes - Annotated - Tree - Functions - Home - Structure Qte

QFontMetrics Class Reference


The QFontMetrics class provides font metrics information about fonts. More...

#include <qfontmetrics.h>

List of all member functions.

Public Members


Detailed Description

The QFontMetrics class provides font metrics information about fonts.

QFontMetrics functions calculate size of characters and strings for a given font. There are three ways you can create a QFontMetrics object:

The QFontMetrics constructor with a QFont creates a font metrics object for a screen-compatible font, i.e. the font can not be a printer font.

QWidget::fontMetrics() returns the font metrics for a widget's font. This is equivalent to QFontMetrics(widget->font()). Setting a new font for the widget later does not affect the font metrics object.

QPainter::fontMetrics() returns the font metrics for a painter's current font. The font metrics object is automatically updated if somebody sets a new painter font (unlike the two above cases, which take a "snapshot" of a font).

Once created, the object provides functions to access the individual metrics of the font, its characters, and for strings rendered in this font.

There are several functions that operate on the font: ascent(), descent(), height(), leading() and lineSpacing() return the basic size properties of the font, and underlinePos(), strikeOutPos() and lineWidth() return properties of the line that underlines 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() and maxWidth(). These are by necessity slow, and we recommend avoiding them if possible.

For each character, you can get its width(), leftBearing() and rightBearing() and find out whether it is in the font using inFont(). You can also treat the character as a string, and use the string functions on it.

The string functions include width(), to return the width of a string in pixels (or points, for a printer), boundingRect(), to return the rectangle necessary to render a string, and size(), to return the size of that rectangle.

Example:

    QFont font("times",24);
    QFontMetrics fm(font);
    int w = fm.width("What's the width of this text");
    int h = fm.height();

See also QFont and QFontInfo.

Examples: movies/main.cpp xform/xform.cpp scrollview/scrollview.cpp grapher/grapher.cpp drawdemo/drawdemo.cpp


Member Function Documentation

QFontMetrics::QFontMetrics ( const QFont & font )

Constructs a font metrics object for font.

The font must be screen-compatible, i.e. a font you use when drawing text in QWidget or QPixmap objects, not QPicture or QPrinter. If font is a printer font, you'll probably get wrong results.

Use QPainter::fontMetrics() to get the font metrics when painting. This is a little slower than using this constructor, but it always gives correct results.

QFontMetrics::QFontMetrics ( const QFontMetrics & fm )

Constructs a copy of fm.

QFontMetrics::~QFontMetrics ()

Destructs the font metrics object.

int QFontMetrics::ascent () const

Returns the maximum ascent of the font.

The ascent is the distance from the base line to the uppermost line where pixels may be drawn.

See also descent().

Examples: scrollview/scrollview.cpp drawdemo/drawdemo.cpp

QRect QFontMetrics::boundingRect ( QChar ch ) const

Returns the bounding rectangle of ch relative to the leftmost 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 cover all pixels in the bounding rectangle.

Note that the rectangle usually extends both above and below the base line.

See also width().

QRect QFontMetrics::boundingRect ( const QString & str, int len = -1 ) const

Returns the bounding rectangle of the first len characters of str, which is the set of pixels the text would cover if drawn at (0,0).

If len is negative (default value), the whole string is used.

Note that the bounding rectangle may extend to the left of (0,0), e.g. for italicized fonts, and that the text output may cover all pixels in the bounding rectangle.

Newline characters are processed as regular characters, not as linebreaks.

Due to the different actual character heights, the height of the bounding rectangle of e.g. "Yes" and "yes" may be different.

See also width() and QPainter::boundingRect().

QRect QFontMetrics::boundingRect ( int x, int y, int w, int h, int flgs, const QString & str, int len=-1, int tabstops=0, int * tabarray=0, char ** intern=0 ) const

Returns the bounding rectangle of the first len characters of str, which is the set of pixels the text would cover if drawn at (0,0). The drawing, and hence the bounding rectangle, is constrained to the rectangle (x,y,w,h).

If len is negative (default value), the whole string is used.

The flgs argument is the bitwise OR of the following flags:

Horizontal alignment defaults to AlignLeft and vertical alignment defaults to AlignTop.

If several of the horizontal or several of the vertical alignment flags are set, the resulting alignment is undefined.

These flags are defined in qnamespace.h.

If ExpandTabs is set in flgs, then: if tabarray is non.zero, it specifies a 0-terminated sequence of pixel-positions for tabs; otherwise if tabstops is non-zero, it is used as the tab spacing (in pixels).

Note that the bounding rectangle may extend to the left of (0,0), e.g. for italicized fonts, and that the text output may cover all pixels in the bounding rectangle.

Newline characters are processed as linebreaks.

Despite the different actual character heights, the heights of the bounding rectangles of "Yes" and "yes" are the same.

The bounding rectangle given by this function is somewhat larger than that calculated by the simpler boundingRect() function. This function uses the maximum left and right font bearings as is necessary for multi-line text to align correctly. Also, fontHeight() and lineSpacing() are used to calculate the height, rather than individual character heights.

The internal argument is for internal purposes.

See also width(), QPainter::boundingRect() and Qt::AlignmentFlags.

Examples: xform/xform.cpp

int QFontMetrics::descent () const

Returns the maximum descent of the font.

The descent is the distance from the base line to the lowermost line where pixels may be drawn. (Note that this is different from X, which adds 1 pixel.)

See also ascent().

Examples: drawdemo/drawdemo.cpp

int QFontMetrics::height () const

Returns the height of the font.

This is always equal to ascent()+descent()+1 (the 1 is for the base line).

See also leading() and lineSpacing().

Examples: grapher/grapher.cpp

bool QFontMetrics::inFont ( QChar ch ) const

Returns TRUE if ch is a valid character in the font.

int QFontMetrics::leading () const

Returns the leading of the font.

This is the natural inter-line spacing.

See also height() and lineSpacing().

int QFontMetrics::leftBearing ( QChar ch ) const

Returns the left bearing of character ch in the font.

The left bearing is the rightward 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 also rightBearing(QChar), minLeftBearing() and width().

int QFontMetrics::lineSpacing () const

Returns the distance from one base line to the next.

This value is always equal to leading()+height().

See also height() and leading().

Examples: scrollview/scrollview.cpp

int QFontMetrics::lineWidth () const

Returns the width of the underline and strike-out lines, adjusted for the point size of the font.

See also underlinePos() and strikeOutPos().

int QFontMetrics::maxWidth () const

Returns the width of the widest character in the font.

int QFontMetrics::minLeftBearing () const

Returns the minimum left bearing of the font.

This is the smallest leftBearing(char) of all characters in the font.

Note that this function can be very slow if the font is big.

See also minRightBearing() and leftBearing(char).

int QFontMetrics::minRightBearing () const

Returns the minimum right bearing of the font.

This is the smallest rightBearing(char) of all characters in the font.

Note that this function can be very slow if the font is big.

See also minLeftBearing() and rightBearing(char).

QFontMetrics & QFontMetrics::operator= ( const QFontMetrics & fm )

Font metrics assignment.

int QFontMetrics::rightBearing ( QChar ch ) const

Returns the right bearing of character ch in the font.

The right bearing is the leftward 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 the width() of the character.

See width() for a graphical description of this metric.

See also leftBearing(char), minRightBearing() and width().

QSize QFontMetrics::size ( int flgs, const QString & str, int len=-1, int tabstops=0, int * tabarray=0, char ** intern=0 ) const

Returns the size in pixels of the first len characters of str.

If len is negative (default value), the whole string is used.

The flgs argument is the bitwise OR of the following flags:

These flags are defined in qnamespace.h.

If ExpandTabs is set in flgs, then: if tabarray is non.zero, it specifies a 0-terminated sequence of pixel-positions for tabs; otherwise if tabstops is non-zero, it is used as the tab spacing (in pixels).

Newline characters are processed as linebreaks.

Despite the different actual character heights, the heights of the bounding rectangles of "Yes" and "yes" are the same.

The intern argument is for internal purposes.

See also boundingRect().

int QFontMetrics::strikeOutPos () const

Returns the distance from the base line to where the strike-out line should be drawn.

See also underlinePos() and lineWidth().

int QFontMetrics::underlinePos () const

Returns the distance from the base line to where an underscore should be drawn.

See also strikeOutPos() and lineWidth().

int QFontMetrics::width ( QChar ch ) const

Returns the logical width of a ch in pixels. This is a distance appropriate for drawing a subsequent character after ch.

Some of the metrics are described in the image to the right. The tall dark rectangle covers the logical width() of a character. The shorter pale rectangles cover leftBearing() and rightBearing() of the characters. Notice that the bearings of "f" in this particular font are both negative, while the bearings of "o" are both positive.

See also boundingRect().

int QFontMetrics::width ( const QString & str, int len = -1 ) const

Returns the width in pixels of the first len characters of str.

If len is negative (the default value is), the whole string is used.

Note that this value is not equal to boundingRect().width(); boundingRect() returns a rectangle describing the pixels this string will cover whereas width() returns the distance to where the next string should be drawn.

See also boundingRect().

Examples: movies/main.cpp scrollview/scrollview.cpp drawdemo/drawdemo.cpp

int QFontMetrics::width ( char c ) const

This function is obsolete. It is provided to keep old source working, and will probably be removed in a future version of Qt. We strongly advise against using it in new code.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Provided to aid porting from Qt 1.x.


Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):


This file is part of the Qt toolkit, copyright © 1995-2005 Trolltech, all rights reserved.


Copyright © 2005 TrolltechTrademarks
Qt version 2.3.10