The QFont class specifies a font used for drawing text in QPainter. (details) (complete member list)
#include <qfont.h>
A QFont has a series of attributes that can be set to specify an abstract font. When actual drawing of text is done Qt will select a font in the underlying window system that matches. the abstract font as close as possible. The most important attributes are family , point size , weight and italic.
One of the QFont constructors take exactly these attributes as arguments:
void YourWidget::paintEvent( QPaintEvent * ) {
QPainter p;
p.begin( 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, 18pt, bold
p.setFont( QFont( "courier", 24, QFont::Bold ) );
p.drawText( 10, 220, "Text3" );
// lucida, 24pt, bold, italic
p.setFont( QFont( "lucida", 36, QFont::Bold, TRUE ) );
p.drawText( 10, 320, "Text4" );
p.end();
}
In general font handling and loading are costly operations that put a heavy load on the window system, this is especially true for X. The QFont class has an internal sharing and reference count mechanism, it has a lazy loading mechanism and does not match and load a font until it really has to, it also caches previously loaded fonts and under X it caches previously matched font attribute combinations.
Note that the functions returning attribute values in QFont return the values previously set not the attributes of the actual window system font used for drawing. To get information about the actual font use QFontInfo.
To get font size information use the class QFontMetrics.
Copy constructor.
Cleans up when a QFont dies.
Returns the CharSet set by setCharSet(), use QFontInfo to find the CharSet of the window system font actually used.
See also: setCharSet() and QFontInfo.
Returns the point size in 1/10ths of a point.
Returns the system default font.
Returns TRUE if a window system font exactly matching the settings of this font is available.
See also: QFontInfo and font matching.
Returns the family name set by setFamily(), use QFontInfo to find the family name of the window system font actually used.
Example:
QFont fnt( "Nairobi" );
QFontInfo info( fnt );
debug( "Font family requested is : \"%s\"", fnt.family() );
debug( "Font family actually used is: \"%s\"", info.family() );
See also: setFamily() and QFontInfo.
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.
Returns the value set by setItalic(), use QFontInfo to find the italic value of the window system font actually used.
See also: setItalic() and QFontInfo.
Returns FALSE if the two QFonts have the same values for all fields, i.e rawMode, pointSize, styleHint, charSet, weight, italic, underline, strikeOut, fixedPitch and family. If the QFonts both are in rawMode() only the family fields are compared.
See also: operator==().
Returns TRUE if the two QFonts have the same values for all fields, i.e rawMode, pointSize, styleHint, charSet, weight, italic, underline, strikeOut, fixedPitch and family. If the QFonts both are in rawMode() only the family fields are compared.
See also: operator!=().
Returns the point size set by setPointSize(), use QFontInfo to find the point size of the window system font actually used.
Example:
QFont fnt( "helvetica" );
QFontInfo info( fnt );
fnt.setPointSize( 53 );
debug( "Font size requested is : \"%i\"", fnt.pointSize() );
debug( "Font size actually used is: \"%i\"", info.pointSize() );
See also: setPointSize() and QFontInfo.
Returns the value set by setRawMode.
See also: setRawMode()
Sets the character set (e.g. Latin1). If the character set is not available another will be used, for most non-trivial applications you will probably not want this to happen since it can totally obscure the text shown to the user when the font is used. This is why the font matching algorithm gives high priority to finding the correct character set.
(Currently using the correct font family has higher priority than using the correct character set. We are not certain if this should be reversed and might do so in the 1.0 release. If you have opinions about this please mail us!)
To ensure that the character set is correct you can use the QFontInfo
class, example:
QFont fnt( "times", 14 ); // default character set is Latin1
QFontInfo info( fnt );
if ( info.charSet() != Latin1 ) // Check info NOT fnt
fatal( "Cannot find a Latin 1 Times font" );
See also: charSet(), QFontInfo and font matching.
Sets the system default font.
Sets the family name of the font (e.g. "Helvetica" or "times"). Case does not matter. If the family is not available a default family will be used instead
See also: family(), setStyleHint(), QFontInfo and font matching.
To do:
Sets fixed pitch on or off. If the mode selected is not available the other will be used. A fixed pitch font is a font that has constant character pixel width. (see QFontInfo and font matching).
See also: fixedPitch(), QFontInfo and font matching.
Sets italic on or off. If the mode selected is not available the other will be used.
See also: italic(), QFontInfo and font matching.
Sets the point size (e.g. 12 or 18). If the point size is not available the closest available will be used
Setting of point sizes less than or equal to 0 will be ignored.
See also: pointSize(), QFontInfo and font matching.
Sets raw mode on or off. This function only has effect under X windows. If raw mode is on Qt will search for an X font with a complete font name matching the family name, all other values set for the QFont will be ignored. 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).
Example:
#if defined(_WS_X11_)
QFont fnt( "-*-fixed-*-*-*-*-*-140-75-75-c-*-iso8859-1" );
fnt.setRawMode( TRUE );
if ( !fnt.exactMatch() )
debug( "Sorry, could not find the X specific font" );
#endif
Warning: Don't use it if you don't need it!
See also: rawMode()
Sets strike out on or off. If the mode selected is not available the other will be used (Both are always available under X windows.).
See also: strikeOut(), QFontInfo and font matching.
Sets the style hint. The style hint is 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 has a default value of AnyStyle which leaves the task of finding a good default family to the font matching algorithm.
In this example (which is a complete program) the pushbutton
will display its text label with the
Bavaria font family if this family is available, if not it will
display its text label with the Times font family:
#include <qapp.h>
#include <qpushbt.h>
#include <qfont.h>
int main( int argc, char **argv )
{
QApplication app( argc, argv );
QPushButton push("Push me");
QFont fnt( "Bavaria", 18 ); // Preferrred family is Bavaria
fnt.setStyleHint( QFont::Times ); // Use Times if Bavaria is not available
push.setFont( fnt );
push.show();
return app.exec( &push );
}
See also: styleHint(), QFontInfo and font matching.
Sets underline on or off. If the mode selected is not available the other will be used. (Both are always available under X windows.).
See also: underline(), QFontInfo and font matching.
Sets the weight (or boldness). The weight must be in the range [0,99] (where 0 is ultralight and 99 is extremely black), the values of the enum type Weight can also be used.
example:
QFont f( "courier" );
f.setWeight( QFont::Bold );
If the weight is not available the closest available will be used. (Use QFontInfo to check.) Setting of weights outside the legal range will be ignored.
Note: The window systems supported by Qt will in practice only support QFont::Normal and QFont::Bold, and, if you are lucky, the other values in Weight.
See also weight(), QFontInfo and font matching.
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.
Returns the StyleHint set by setStyleHint().
See also: setStyleHint().
Returns the value set by setUnderline(), use QFontInfo to find the underline value of the window system font actually used.
See also: setUnderline() and QFontInfo.
Returns the weight set by setWeight(), use QFontInfo to find the weight of the window system font actually used.
See also: setWeight() and QFontInfo.
This file is part of the Qt toolkit, copyright 1995 Troll Tech, all rights reserved.
It was generated from the following files: