|  | Home · Overviews · Examples

# QFont Class Reference
[com.trolltech.qt.gui module]

The `QFont` class specifies a font used for drawing text. More...

## Detailed Description

The `QFont` class specifies a font used for drawing text.

When you create a `QFont` object you specify various attributes that you want the font to have. Qt will use the font with the specified attributes, or if no matching font exists, Qt will use the closest matching installed font. The attributes of the font that is actually used are retrievable from a `QFontInfo` object. If the window system provides an exact match `exactMatch` returns true. Use `QFontMetrics` to get measurements, e.g. the pixel length of a string using QFontMetrics::width().

Use QApplication::setFont() to set the application's default font.

If a chosen font does not include all the characters that need to be displayed, `QFont` will try to find the characters in the nearest equivalent fonts. When a `QPainter` draws a character from a font the `QFont` will report whether or not it has the character; if it does not, `QPainter` will draw an unfilled square.

Create QFonts like this:

        QFont serifFont("Times", 10, QFont::Bold);
        QFont sansFont("Helvetica [Cronyx]", 12);

The attributes set in the constructor can also be set later, e.g. `setFamily`, `setPointSize`, setPointSizeFloat(), `setWeight` and `setItalic`. The remaining attributes must be set after contstruction, e.g. `setBold`, `setUnderline`, `setOverline`, `setStrikeOut` and `setFixedPitch`. `QFontInfo` objects should be created after the font's attributes have been set. A `QFontInfo` object will not change, even if you change the font's attributes. The corresponding "get" functions, e.g. `family`, `pointSize`, etc., return the values that were set, even though the values used may differ. The actual values are available from a `QFontInfo` object.

If the requested font family is unavailable you can influence the font matching algorithm by choosing a particular `QFont::StyleHint` and `QFont::StyleStrategy` with `setStyleHint`. The default family (corresponding to the current style hint) is returned by `defaultFamily`.

The font-matching algorithm has a `lastResortFamily` and `lastResortFont` in cases where a suitable match cannot be found. You can provide substitutions for font family names using `insertSubstitution` and `insertSubstitutions`. Substitutions can be removed with `removeSubstitution`. Use `substitute` to retrieve a family's first substitute, or the family name itself if it has no substitutes. Use `substitutes` to retrieve a list of a family's substitutes (which may be empty).

Every `QFont` has a `key` which you can use, for example, as the key in a cache or dictionary. If you want to store a user's font preferences you could use `QSettings`, writing the font information with `toString` and reading it back with `fromString`. The operator<<() and operator>>() functions are also available, but they work on a data stream.

It is possible to set the height of characters shown on the screen to a specified number of pixels with `setPixelSize`; however using `setPointSize` has a similar effect and provides device independence.

Under X11 you can set a font using its system specific name with `setRawName`.

Loading fonts can be expensive, especially on X11. `QFont` contains extensive optimizations to make the copying of `QFont` objects fast, and to cache the results of the slow window system functions it depends upon.

The font matching algorithm works as follows:

1. The specified font family is searched for.
2. If not found, the `styleHint` is used to select a replacement family.
3. Each replacement font family is searched for.
4. If none of these are found or there was no `styleHint`, "helvetica" will be searched for.
5. If "helvetica" isn't found Qt will try the `lastResortFamily`.
6. If the `lastResortFamily` isn't found Qt will try the `lastResortFont` which will always return a name of some kind.

Note that the actual font matching algorithm varies from platform to platform.

Once a font is found, the remaining attributes are matched in order of priority:

1. `fixedPitch`
2. `pointSize` (see below)
3. `weight`
4. `style`

If you have a font which matches on family, even if none of the other attributes match, this font will be chosen in preference to a font which doesn't match on family but which does match on the other attributes. This is because font family is the dominant search criteria.

The point size is defined to match if it is within 20% of the requested point size. When several fonts match and are only distinguished by point size, the font with the closest point size to the one requested will be chosen.

The actual family, font size, weight and other font attributes used for drawing text will depend on what's available for the chosen family under the window system. A `QFontInfo` object can be used to determine the actual values used for drawing the text.

Examples:

        QFont f("Helvetica");

If you had both an Adobe and a Cronyx Helvetica, you might get either.

        QFont f("Helvetica [Cronyx]");

You can specify the foundry you want in the family name. The font f in the above example will be set to "Helvetica [Cronyx]".

To determine the attributes of the font actually used in the window system, use a `QFontInfo` object, e.g.

        QFontInfo info(f1);
        QString family = info.family();

To find out font metrics use a `QFontMetrics` object, e.g.

        QFontMetrics fm(f1);
        int textWidthInPixels = fm.width("How many pixels wide is this text?");
        int textHeightInPixels = fm.height();

For more general information on fonts, see the comp.fonts FAQ. Information on encodings can be found from Roman Czyborra's page.

See also `QFontComboBox`, `QFontMetrics`, `QFontInfo`, `QFontDatabase`, and Character Map Example.