| Classes - Annotated - Tree - Functions - Home - Structure

# QFont Class Reference

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

`#include <qfont.h>`

List of all member functions.

## Public Members

- enum CharSet { ISO_8859_1, Latin1 = ISO_8859_1, AnyCharSet, ISO_8859_2, Latin2 = ISO_8859_2, ISO_8859_3, Latin3 = ISO_8859_3, ISO_8859_4, Latin4 = ISO_8859_4, ISO_8859_5, ISO_8859_6, ISO_8859_7, ISO_8859_8, ISO_8859_9, Latin5 = ISO_8859_9, ISO_8859_10, Latin6 = ISO_8859_10, ISO_8859_11, TIS620 = ISO_8859_11, ISO_8859_12, ISO_8859_13, Latin7 = ISO_8859_13, ISO_8859_14, Latin8 = ISO_8859_14, ISO_8859_15, Latin9 = ISO_8859_15, KOI8R, Set_Ja, Set_1 = Set_Ja, Set_Ko, Set_Th_TH, Set_Zh, Set_Zh_TW, Set_N = Set_Zh_TW, Unicode, Set_GBK, Set_Big5, TSCII, KOI8U, CP1251, PT154, JIS_X_0201 = 0xa0, JIS_X_0208 = 0xc0, Enc16 = JIS_X_0208, KSC_5601, GB_2312, Big5 }
- enum StyleHint { Helvetica, Times, Courier, OldEnglish, System, AnyStyle, SansSerif = Helvetica, Serif = Times, TypeWriter = Courier, Decorative = OldEnglish}
- enum StyleStrategy { PreferDefault = 0x0001, PreferBitmap = 0x0002, PreferDevice = 0x0004, PreferOutline = 0x0008, ForceOutline = 0x0010, PreferMatch = 0x0020, PreferQuality = 0x0040 }
- enum Weight { Light = 25, Normal = 50, DemiBold = 63, Bold = 75, Black = 87 }
- QFont ()
- QFont ( const QString & family, int pointSize = 12, int weight = Normal, bool italic = FALSE )
- QFont ( const QString & family, int pointSize, int weight, bool italic, CharSet charSet )
- QFont ( const QFont & )
- ~QFont ()
- QFont& operator= ( const QFont & )
- QString family () const
- void setFamily ( const QString & )
- int pointSize () const
- float pointSizeFloat () const
- void setPointSize ( int )
- void setPointSizeFloat ( float )
- int pixelSize () const
- void setPixelSize ( int )
- void setPixelSizeFloat ( float )
- int weight () const
- void setWeight ( int )
- bool bold () const
- void setBold ( bool )
- bool italic () const
- void setItalic ( bool )
- bool underline () const
- void setUnderline ( bool )
- bool strikeOut () const
- void setStrikeOut ( bool )
- bool fixedPitch () const
- void setFixedPitch ( bool )
- StyleHint styleHint () const
- void setStyleHint ( StyleHint )
- StyleStrategy styleStrategy () const
- void setStyleHint ( StyleHint, StyleStrategy )
- CharSet charSet () const
- void setCharSet ( CharSet )
- bool rawMode () const
- void setRawMode ( bool )
- bool exactMatch () const
- bool operator== ( const QFont & ) const
- bool operator!= ( const QFont & ) const
- bool isCopyOf ( const QFont & ) const
- void setRawName ( const QString & )
- QString rawName () const
- QString key () const

## Static Public Members

- CharSet charSetForLocale ()
- QString encodingName ( CharSet )
- QFont defaultFont ()
- void setDefaultFont ( const QFont & ) (obsolete)
- QString substitute ( const QString & familyName )
- void insertSubstitution ( const QString &, const QString & )
- void removeSubstitution ( const QString & )
- QStringList substitutions ()
- void initialize ()
- void locale_init ()
- void cleanup ()
- void cacheStatistics ()

## Protected Members

- bool dirty () const
- QString defaultFamily () const
- QString lastResortFamily () const
- QString lastResortFont () const
- int deciPointSize () const

## Related Functions

(Note that these are not member functions.)

- QDataStream & operator<< (QDataStream & s, const QFont & f)
- QDataStream & operator>> (QDataStream & s, QFont & f)

## Detailed Description

The QFont class specifies a font used for drawing text.

QFont, more precisely, is a collection of attributes of a font. When Qt needs to draw text, it will look up and load the closest matching installed font and draw using that.

The most important attributes of a QFont are its family(), pointSize(), weight() and whether it is italic() or not. There are QFont constructors that take these attributes as arguments, as shown in this example:

        void MyWidget::paintEvent( QPaintEvent * )
        {
            QPainter p( this );

            // times, 12pt, normal
            p.setFont( QFont( "times" ) );
            p.drawText( 10, 20, "Text1" );

            // helvetica, 18pt, normal
            p.setFont( QFont( "helvetica", 18 ) );
            p.drawText( 10, 120, "Text2" );

            // courier, 24pt, bold
            p.setFont( QFont( "courier", 24, QFont::Bold ) );
            p.drawText( 10, 220, "Text3" );

            // lucida, 36pt, bold, italic
            p.setFont( QFont( "lucida", 36, QFont::Bold, TRUE ) );
            p.drawText( 10, 320, "Text4" );
        }

The default QFont constructor makes a copy of application's default font, QApplication::font().

You can also change these attributes of an existing QFont object using functions such as setFamily(), setPointSize(), setWeight() and setItalic().

There are also some less-used attributes. setUnderline() decides whether the font is underlined or not; setStrikeOut() can be used to get overstrike (a horizontal line through the middle of the characters); setFixedPitch() determines whether Qt should give preference to fixed-pitch (also known as fixed-width) or variable-pitch fonts when it needs to choose an installed font; setStyleHint() can be used to offer more general help to the font matching algorithm, and on X11 setRawName() can be used to bypass the entire font matching and use an X11 XLFD.

Of course there is also a reader function for each of these set*() functions. Note that the reader functions return the values previously set, not the attributes of the actual window system font that will be used for drawing. You can get information about the font that will be used for drawing by using QFontInfo, but be aware that QFontInfo may be slow and that its results depend on what fonts are installed.

In general font handling and loading are costly operations, especially on X11. The QFont class contains extensive optimizations to make copying of QFont objects fast, and to cache the results of the slow window system functions it uses.

QFont also offers a few static functions, mostly to tune the font matching algorithm: You can control what happens if a font's family isn't installed using insertSubstitution() and removeSubstitution(), ask what happens for a single family using substitute() and you can get a complete list of the fallback families using substitutions().

cacheStatistics() offers cache effectiveness information; this is useful mostly for debugging.

Finally, QApplication::setFont() allows you to set the default font. The default default font is chosen at application startup from a set of common installed fonts that support the correct character set for the current locale. Of course, the initialization algorithm has a default, too: The default default default font!

The font matching algorithm works as follows:

First an available font family is found. If the requested is not available the styleHint() is used to select a replacement family. If the style hint has not been set, "helvetica" will be used.

If even the replacement family is not found, "helvetica" is searched for, if that too is not found Qt will search for a last resort font, i.e. a specific font to match to, ignoring the attribute settings. Qt searches through a built-in list of very common fonts. If none of these are available, Qt gives you an error message and aborts (of course this only happens if you are using fonts and Qt has to load a font). We have not been able to find a case where this happens. Please report it as a bug if it does, preferably with a list of the fonts you have installed.

The following attributes are then matched, in order of priority:

1. charSet()
2. fixedPitch()
3. pointSize() (see below)
4. weight()
5. italic()

If, for example, a font with the correct character set is found, but with all other attributes in the list unmatched, it will be chosen before a font with the wrong character set but with all other attributes correct.

The point size is defined to match if it is within 20% of the requested point size. Of course, when several fonts match and only point size differs the closest point size to the one requested will be chosen.

For more general information on fonts, see the comp.fonts FAQ and for more general information on encodings, see Roman Czyborra's page about that.

See also QFontMetrics, QFontInfo, QApplication::setFont(), QWidget::setFont(), QPainter::setFont(), QFont::StyleHint, QFont::CharSet and QFont::Weight.

Examples: showimg/main.cpp grapher/grapher.cpp xform/xform.cpp i18n/main.cpp drawdemo/drawdemo.cpp menu/menu.cpp desktop/desktop.cpp scrollview/scrollview.cpp qfd/qfd.cpp movies/main.cpp picture/picture.cpp hello/main.cpp

## Member Type Documentation

### QFont::CharSet

The following character set encodings are available:

- `QFont::ISO_8859_1` - Latin1 , common in much of Europe
- `QFont::ISO_8859_2` - Latin2, Central and Eastern European character set
- `QFont::ISO_8859_3` - Latin3, less common European character set
- `QFont::ISO_8859_4` - Latin4, less common European character set
- `QFont::ISO_8859_5,` Cyrillic
- `QFont::ISO_8859_6,` Arabic
- `QFont::ISO_8859_7,` Greek
- `QFont::ISO_8859_8,` Hebrew
- `QFont::ISO_8859_9,` Turkish
- `QFont::ISO_8859_10..15,` other ISO 8859 characters sets
- `QFont::KOI8R` - KOI8-R, Cyrillic, defined in RFC 1489.
- `QFont::KOI8U` - KOI8-U, Cyrillic/Ukrainian, defined in RFC 2319.
- `QFont::CP1251,` Microsoft Cyrillic encoding
- `QFont::PT154,` Paratype Asian Cyrillic encoding
- `QFont::AnyCharSet` - whatever is handiest.
- `QFont::Set_Ja,` Japanese
- `QFont::Set_Ko,` Korean
- `QFont::Set_Th_TH`
- `QFont::Set_Zh`
- `QFont::Set_Zh_TW`
- `QFont::Unicode,` Unicode character set
- `QFont::Set_GBK`
- `QFont::Set_Big5`

### QFont::StyleHint

Style hints are used by the font matching algorithm when a selected font family cannot be found and is used to find an appropriate default family.

The style hint value of `AnyStyle` leaves the task of finding a good default family to the font matching algorithm.

The other available style hints are `QFont::SansSerif, QFont::TypeWriter, QFont::OldEnglish, QFont::System`

### QFont::StyleStrategy

The style strategy tells the font matching algorithm what type of fonts should be used to find an appropriate default family.

The algorithm won't prefer any type of font if `NoStratgie` is provided.

The other available strategys are `QFont::PreferBitmap, QFont::PreferDevice, QFont::PreferOutline, QFont::ForceOutline`

Any of these may be ORed with a indicator whether exact matching or good quality should be preferred.

`QFont::PreferMatch, QFont::PreferQuality`

### QFont::Weight

Contains the predefined font weights:

- `QFont::Light` (25)
- `QFont::Normal` (50)
- `QFont::DemiBold` (63)
- `QFont::Bold` (75)
- `QFont::Black` (87)

## Member Function Documentation

### QFont::QFont ()

Constructs a font object that refers to the default font.

See also QApplication::setFont() and QApplication::font().

### QFont::QFont ( const QString & family, int pointSize = 12, int weight = Normal, bool italic = FALSE )

Constructs a font object with the specified family, pointSize, weight and italic settings. The charSet() is copied from the default font and the rest of the settings are set reasonably.

If pointSize is less than or equal to 0 it is set to 1.

See also setFamily(), setPointSize(), setWeight() and setItalic().

### QFont::QFont ( const QString & family, int pointSize, int weight, bool italic, CharSet charSet )

Constructs a font object with the specified family, pointSize, weight, italic and charSet settings. If pointSize is less than or equal to 0 it is set to 1.

See also setFamily(), setPointSize(), setWeight() and setItalic().

### QFont::QFont ( const QFont & font )

Constructs a font that is a copy of font.

### QFont::~QFont ()

Destructs the font object.

### bool QFont::bold () const

Returns TRUE if weight() is a value greater than `QFont::Normal,` otherwise FALSE.

See also weight(), setBold() and QFontInfo::bold().

### void QFont::cacheStatistics () `[static]`

Internal function that dumps font cache statistics.

### QFont::CharSet QFont::charSet() const

Returns the character set by setCharSet().

Use QFontInfo to find the CharSet of the window system font actually used.

See also setCharSet().

### QFont::CharSet QFont::charSetForLocale() `[static]`

Returns a guess at the character set most likely to be appropriate for the locale the application is running in. This is the character sets fonts will use by default.

### void QFont::cleanup () `[static]`

Internal function that cleans up the font system.

### int QFont::deciPointSize () const `[protected]`

Returns the point size in 1/10ths of a point.

See also pointSize().

### QString QFont::defaultFamily () const `[protected]`

Returns the family name that corresponds to the current style hint.

### QFont QFont::defaultFont () `[static]`

Returns the QApplication default font.

This function will be removed in a future version of Qt. Please use QApplication::font() instead.

### bool QFont::dirty () const `[protected]`

Returns TRUE if the font attributes have been changed and the font has to be (re)loaded, or FALSE if no changes have been made.

### QString QFont::encodingName ( CharSet cs ) `[static]`

Returns the encoding name of a character set, e.g. QFont::ISO_8859_1 returns "iso8859-1" and QFont::Unicode returns "iso10646".

### bool QFont::exactMatch () const

Returns TRUE if a window system font exactly matching the settings of this font is available.

See also QFontInfo.

### QString QFont::family () const

Returns the family name set by setFamily().

Use QFontInfo to find the family name of the window system font that is actually used for drawing.

Example:

        QFont     font( "Nairobi" );
        QFontInfo info( font );
        qDebug( "Font family requested is    : \"%s\"", font.family() );
        qDebug( "Font family actually used is: \"%s\"", info.family() );

See also setFamily() and substitute().

### bool QFont::fixedPitch () const

Returns the value set by setFixedPitch().

Use QFontInfo to find the fixed pitch value of the window system font actually used.

See also setFixedPitch() and QFontInfo::fixedPitch().

### void QFont::initialize () `[static]`

Internal function that initializes the font system.

### void QFont::insertSubstitution ( const QString & familyName, const QString & replacementName ) `[static]`

Inserts a new font family name substitution in the family substitution table.

If familyName already exists in the substitution table, it will be replaced with this new substitution.

See also removeSubstitution(), substitutions() and substitute().

### bool QFont::isCopyOf ( const QFont & f ) const

Returns TRUE if this font and f are copies of each other, i.e. one of them was created as a copy of the other and neither was subsequently modified. This is much stricter than equality.

See also operator= and operator==.

### bool QFont::italic () const

Returns the value set by setItalic().

Use QFontInfo to find the italic value of the window system font actually used.

See also setItalic().

### QString QFont::key () const

Returns the font's key, which is a textual representation of the font settings. It is typically used to insert and find fonts in a dictionary or a cache.

See also QMap.

### QString QFont::lastResortFamily () const `[protected]`

Returns a last resort family name for the font matching algorithm.

See also lastResortFont().

### QString QFont::lastResortFont () const `[protected]`

Returns a last resort raw font name for the font matching algorithm. This is used if even the last resort family is not available. It returns something, almost no matter what.

The current implementation tries a wide variety of common fonts, returning the first one it finds. The implementation may change at any time.

See also lastResortFamily().

### void QFont::locale_init () `[static]`

Internal function that uses locale information to find the preferred character set of loaded fonts.

### bool QFont::operator!= ( const QFont & f ) const

Returns TRUE if the this font is different from f, or FALSE if they are equal.

Two QFonts are different if their font attributes are different. If rawMode() is enabled for both fonts, then only the family fields are compared.

See also operator==().

### QFont & QFont::operator= ( const QFont & font )

Assigns font to this font and returns a reference to this font.

### bool QFont::operator== ( const QFont & f ) const

Returns TRUE if the this font is equal to f, or FALSE if they are different.

Two QFonts are equal if their font attributes are equal. If rawMode() is enabled for both fonts, then only the family fields are compared.

See also operator!=().

### int QFont::pixelSize () const

Returns the logical pixel height of characters in the font if shown on the screen.

### int QFont::pointSize () const

Returns the point size set by setPointSize().

Use QFontInfo to find the point size of the window system font actually used.

Example of use:

        QFont     font( "helvetica" );
        QFontInfo info( font );
        font.setPointSize( 53 );
        qDebug( "Font size requested is    : %d", font.pointSize() );
        qDebug( "Font size actually used is: %d", info.pointSize() );

See also setPointSize() and deciPointSize().

### float QFont::pointSizeFloat () const

Returns the height of characters in the font in points (1/72 inch).

See also pointSize().

### bool QFont::rawMode () const

Returns the value set by setRawMode().

See also setRawMode().

### QString QFont::rawName () const

Returns the name of the font within the underlying window system. On Windows, this is usually just the family name of a true type font. Under X, it is a rather complex XLFD (X Logical Font Description). Using the return value of this function is usually not portable.

See also setRawName().

### void QFont::removeSubstitution ( const QString & familyName ) `[static]`

Removes a font family name substitution from the family substitution table.

See also insertSubstitution(), substitutions() and substitute().

### void QFont::setBold ( bool enable )

Sets the weight to `QFont::Bold` if enable is TRUE, or to `QFont::Normal` if enable is FALSE.

Use setWeight() to set the weight to other values.

See also bold() and setWeight().

Examples: menu/menu.cpp

### void QFont::setCharSet ( CharSet charset )

Sets the character set encoding (e.g. `Latin1).`

If the character set encoding is not available another will be used for drawing. For most non-trivial applications you will probably not want this to happen since it can totally obscure the text shown to the user. This is why the font matching algorithm gives high priority to finding the correct character set.

You can test that the character set is correct using the QFontInfo class.

See also charSet() and QFontInfo.

### void QFont::setDefaultFont ( const QFont & f ) `[static]`

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.

Please use QApplication::setFont() instead.

### void QFont::setFamily ( const QString & family )

Sets the family name of the font (e.g. "Helvetica" or "times").

The family name is case insensitive.

If the family is not available a default family is used.

See also family(), setStyleHint() and QFontInfo.

### void QFont::setFixedPitch ( bool enable )

Sets fixed pitch on or off.

A fixed pitch font is a font where all characters have the same width.

See also fixedPitch() and QFontInfo.

### void QFont::setItalic ( bool enable )

Sets italic on or off.

See also italic() and QFontInfo.

### void QFont::setPixelSize ( int pixelSize )

Sets the logical height of characters in the font if shown on the screen.

### void QFont::setPixelSizeFloat ( float pixelSize )

Sets the logical pixel height of characters in the font if shown on the screen.

### void QFont::setPointSize ( int pointSize )

Sets the point size to pointSize. The point size must be greater than zero.

Example:

        QFont font( "courier" );
        font.setPointSize( 18 );

See also pointSize() and QFontInfo.

### void QFont::setPointSizeFloat ( float pointSize )

Sets the point size to pointSize. The point size must be greater than zero. The requested precision may not be achieved on all platforms.

### void QFont::setRawMode ( bool enable )

Turns raw mode on if enable is TRUE, or turns it off if enable is FALSE.

Calling this function only has effect under X windows. If raw mode is enabled, Qt will search for an X font with a complete font name matching the family name, ignoring all other values set for the QFont. If the font name matches several fonts, Qt will use the first font returned by X. QFontInfo cannot be used to fetch information about a QFont using raw mode (it will return the values set in the QFont for all parameters, including the family name).

Warning: Do not use raw mode unless you really, really need it! In most (if not all) cases, setRawName() is a much better choise.

See also rawMode() and setRawName().

### void QFont::setRawName ( const QString & name )

Sets a font by its system specific name. The function is in particular useful under X, where system font settings ( for example X resources) are usually available as XLFD (X Logical Font Description) only. You can pass an XLFD as name to this function.

In Qt 2.0 and later, a font set with setRawName() is still a full-featured QFont. It can be queried (for example with italic()) or modified (for example with setItalic() ) and is therefore also suitable as a basis font for rendering rich text.

If Qt's internal font database cannot resolve the raw name, the font becomes a raw font with name as family.

Note that the present implementation does not handle handle wildcards in XLFDs well, and that font aliases (file `fonts.alias` in the font directory on X11) are not supported.

See also rawName(), setRawMode() and setFamily().

### void QFont::setStrikeOut ( bool enable )

Sets strike out on or off.

See also strikeOut() and QFontInfo.

### void QFont::setStyleHint ( StyleHint hint )

Sets the style hint.

See also setStyleHint().

Examples: desktop/desktop.cpp

### void QFont::setStyleHint ( StyleHint hint, StyleStrategy strategy )

Sets the style hint and strategy.

The style hint has a default value of `AnyStyle` which leaves the task of finding a good default family to the font matching algorithm.

The style strategy has a default value of `PreferDefault` which tells the algorithm not to prefer any type of font.

In the example below the push button will display its text label with the Bavaria font family if this family is available, if not it will display its text label with another serif font:

        #include <qapplication.h>
        #include <qpushbutton.h>
        #include <qfont.h>

        int main( int argc, char **argv )
        {
            QApplication app( argc, argv );
            QPushButton  push("Push me");

            QFont font( "Bavaria", 18 );        // preferred family is Bavaria
            font.setStyleHint( QFont::Serif )    // can also use any serif font

            push.setFont( font );
            return app.exec( &push );
        }

See also QFont::StyleHint, styleHint(), QFont::StyleStrategy, styleStrategy() and QFontInfo.

### void QFont::setUnderline ( bool enable )

Sets underline on or off.

See also underline() and QFontInfo.

Examples: menu/menu.cpp

### void QFont::setWeight ( int weight )

Sets the weight (or boldness), which should be a value from the QFont::Weight enumeration.

Example:

        QFont font( "courier" );
        font.setWeight( QFont::Bold );

Strictly speaking you can use all values in the range [0,99] (where 0 is ultralight and 99 is extremely black), but there is perhaps asking too much of the underlying window system.

If the specified weight is not available the closest available will be used. Use QFontInfo to check the actual weight.

See also weight() and QFontInfo.

### bool QFont::strikeOut () const

Returns the value set by setStrikeOut().

Use QFontInfo to find the strike out value of the window system font actually used.

See also setStrikeOut() and QFontInfo::strikeOut().

### QFont::StyleHint QFont::styleHint() const

Returns the StyleHint set by setStyleHint().

See also setStyleHint() and QFontInfo::styleHint().

### QFont::StyleStrategy QFont::styleStrategy() const

Returns the StyleStratgie set by setStyleHint()

See also setStyleHint().

### QString QFont::substitute ( const QString & familyName ) `[static]`

Returns the font family name to be used whenever familyName is specified. The lookup is case insensitive.

If there is no substitution for familyName, then familyName is returned.

Example:

        QFont::insertSubstitution( "NewYork", "London" );
        QFont::insertSubstitution( "Paris",   "Texas" );

        QFont::substitute( "NewYork" );     // returns "London"
        QFont::substitute( "PARIS" );       // returns "Texas"
        QFont::substitute( "Rome" );        // returns "Rome"

        QFont::removeSubstitution( "newyork" );
        QFont::substitute( "NewYork" );     // returns "NewYork"

See also setFamily(), insertSubstitution() and removeSubstitution().

### QStringList QFont::substitutions () `[static]`

Returns a sorted list of substituted family names.

See also insertSubstitution(), removeSubstitution() and substitute().

### bool QFont::underline () const

Returns the value set by setUnderline().

Use QFontInfo to find the underline value of the window system font actually used for drawing.

See also setUnderline() and QFontInfo::underline().

### int QFont::weight () const

Returns the weight set by setWeight().

Use QFontInfo to find the weight of the window system font actually used.

See also setWeight() and QFontInfo.

## Related Functions

### QDataStream & operator<< (QDataStream & s, const QFont & f)

Writes a font to the stream.

See also Format of the QDataStream operators

### QDataStream & operator>> (QDataStream & s, QFont & f)

Reads a font from the stream.

See also Format of the QDataStream operators

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

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