![]() |
| ||
Classes - Annotated - Tree - Functions - Home - Structure |
The QFont class specifies a font used for drawing text. More...
#include <qfont.h>
The QFont class specifies a font used for drawing text.
More precisely, QFont is a collection of attributes of a font. When Qt needs to draw text, it will look up the closest matching installed font, load it and use it. If a choosen X11 font does not cover all characters to be displayed, QFont blends in the missing characters from other fonts if possible.
The most important attributes of a QFont are its font family (family()), its size (pointSize()), its boldness (weight()) and whether it is italic() or not. There are QFont constructors that take these attributes as arguments, for example:
defaultButton = new QPushButton( "Default", this, "pushbutton1" ); defaultButton->setFont( QFont( "times" ) );
12pt (the default if nothing else is specified) Times, normal weight, roman (i.e. non-italic) is used for the label of the defaultButton push button.
sansSerifButton = new QPushButton( "Sans Serif", this, "pushbutton2" ); sansSerifButton->setFont( QFont( "Helvetica", 12 ) );
This button's label is drawn using Helvetica 12pt, non-bold, non-italic.
italicsButton = new QPushButton( "Italics", this, "pushbutton3" ); italicsButton->setFont( QFont( "lucida", 12, QFont::Bold, TRUE ) );
To draw this button's label 12pt Lucida, bold italic is used.
(Code taken from fonts/simple-qfont-demo/viewer.cpp)
The default QFont constructor without any arguments keeps a copy of the application's default font, QApplication::font().
You can change the 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 monospace, fixed-width or typewriter fonts) or variable-pitch (proportional) 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 (X Logical Font Description).
Of course there is also a reader function for each of the 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. Information about the font that will be used for drawing can be obtained by using QFontInfo, but be aware that QFontInfo may be slow and that its results depend on which 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 family isn't installed using insertSubstitution(), insertSubstitutions() and removeSubstitution().
This is especially important when different character sets are used at the same time. As a Times font for example does not include Arabic characters, an Arabic substitution font for Times can be specified and will be used when Arabic characters show up.
Each QFont can have an entire substitution list so that Unicode characters can be displayed even if no Unicode fonts are available. How good this approximation works depends on the variety of fonts installed.
You can get a complete list of the fallback families using substitutions().
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:
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.
You can also specify a font size in pixels (with the setPixelSize() method), but usually this is not recommended, as the pixel size is device dependent. A font with a point size of 72 points will have a size of approximately one inch on all devices (screen or printer), whereas a font with a size of 12 pixels will look the same as the 12 point font on a 75dpi display, but will have a very small size on a 600dpi printer.
For more general information on fonts, see the comp.fonts FAQ. Information on encodings can be found on Roman Czyborra's respective page.
See also QFontMetrics, QFontInfo, QFontDatabase, QApplication::setFont(), font, QPainter::setFont(), QFont::StyleHint and QFont::Weight.
The QFont::Script enum represents Unicode allocated scripts. For exhaustive coverage see The Unicode Standard Version 3.0. The following scripts are supported:
Modern European alphabetic scripts (left to right):
Middle Eastern scripts (right to left):
South and Southeast Asian scripts (left to right with few historical exceptions):
East Asian scripts (traditionally top-down, right to left, modern often horizontal left to right):
Additional scripts that do not fit well into the script categories above:
Symbols:
The values below are provided for completeness and must not be used in user programs.
Style hints are used by the font matching algorithm to find an appropriate default family if a selected font family is not available.
The style strategy tells the font matching algorithm what type of fonts should be used to find an appropriate default family.
The following strategies are available:
Any of these may be ORed with an indicator whether
Whilst all strategies work on Windows, PreferDefault and PreferBitmap are the only ones currently supported with X11.
The weight of a font is a measure for its thickness.
Qt uses a weighting scale from 0 to 99 similar but not equal to the scales used in Windows or CSS. A thickness of 0 leads to an ultralight, 99 to an extremely black font.
This enum contains the predefined font weights:
See also QApplication::setFont() and QApplication::font().
If pointSize is less than or equal to 0 it is set to 1.
See also Weight, setFamily(), setPointSize(), setWeight(), setItalic() and QApplication::font().
Returns TRUE if weight() is a value greater than QFont::Normal, otherwise FALSE.
See also weight(), setBold() and QFontInfo::bold().
The returned value will be -1 if the font size has been specified in pixels.
See also pointSize().
See also StyleHint, styleHint() and setStyleHint().
Please use QApplication::font() instead.
See also QFontInfo.
Use QFontInfo to find the family name of the window system font that is actually used for drawing.
QString messageText; messageText = "Font requested: \"" + font.family() + "\" " +
"Font used: \"" + info.family() + "\" " + QString::number( info.pointSize() ) + "pt<P>";
(Code taken from fonts/simple-qfont-demo/viewer.cpp)
See also substitutes and substitute().
Example: fonts/simple-qfont-demo/viewer.cpp.
QFontInfo reveals whether the window system font actually used fits this requirement.
See also setFixedPitch() and QFontInfo::fixedPitch().
Example: i18n/main.cpp.
QFont::insertSubstitution( "Tokyo", "Lucida" );
(Code taken from fonts/simple-qfont-demo/viewer.cpp)
The family name substituteName is appended to the substitution list for familyName only if it does not already exist in the substitution table. If substituteName is the first substitute, a new substitution table is automatically created.
See also insertSubstitutions(), removeSubstitution(), substitutions(), substitute() and substitutes().
Example: fonts/simple-qfont-demo/viewer.cpp.
QStringList substitutes;
substitutes << "Arabic Newspaper" << "crox";
QFont::insertSubstitutions( "Bavaria", substitutes );
(Code taken from fonts/simple-qfont-demo/viewer.cpp)
A member of substituteNames is appended to the substitution list for familyName only if it does not already exist in the substitution table. In case of a previously non-existing substitution table a new one is created automatically.
See also insertSubstitution, removeSubstitution, substitutions() and substitute().
Example: fonts/simple-qfont-demo/viewer.cpp.
See also operator= and operator==.
Use QFontInfo to find out whether the window system font actually used is italic.
See also setItalic().
See also QMap.
See also lastResortFont().
The current implementation tries a wide variety of common fonts, returning the first one it finds. This implementation may change at any time.
See also lastResortFamily() and rawName().
Two QFonts are different if their font attributes are different. If rawMode() is enabled for both fonts, only the family fields are compared.
See also operator==().
Two QFonts are equal if their font attributes are equal. If rawMode() is enabled for both fonts, only the family fields are compared.
See also operator!=() and isCopyOf().
Use the QFontInfo class to get the real height of the font.
See also setPixelSize(), pointSize(), QFontInfo::pointSize() and QFontInfo::pixelSize().
Use QFontInfo to find the point size of the window system font actually used:
QString messageText; messageText = "Font requested: \"" +
QString::number( font.pointSize() ) + "pt<BR>" + "Font used: \"" +
QString::number( info.pointSize() ) + "pt<P>";
(Code taken from fonts/simple-qfont-demo/viewer.cpp)
The returned value will be -1 if the font size has been specified in pixels.
See also setPointSize() and deciPointSize().
Example: fonts/simple-qfont-demo/viewer.cpp.
See also pointSize(), pixelSize(), QFontInfo::pointSize() and QFontInfo::pixelSize().
See also setRawMode() and rawName().
See also setRawName().
See also insertSubstitutions(), insertSubstitution(), substitutions() and substitute().
Sets the weight to QFont::Bold if enable is TRUE, or to QFont::Normal if enable is FALSE.
Use setWeight() to set the boldness of this font to other values.
See also bold() and setWeight().
Examples: menu/menu.cpp and themes/metal.cpp.
Please use QApplication::setFont() instead.
The family name is case insensitive.
If the family is not available a default family is used.
See also family(), setStyleHint() and QFontInfo.
A fixed pitch font is a font where all characters have the same width.
See also fixedPitch() and QFontInfo.
See also italic() and QFontInfo.
Examples: fileiconview/qfileiconview.cpp, fonts/simple-qfont-demo/viewer.cpp and themes/metal.cpp.
This way of specifying a font size makes the font device dependent, and the resulting font will vary in size on devices with different resolutions. setPointSize() is the device independent approach.
See also pixelSize().
Example: qwerty/qwerty.cpp.
Sets the logical pixel height of font characters when shown on the screen to pixelSize.
QFont font( "Tokyo" ); font.setPointSize( 32 );
(Code taken from fonts/simple-qfont-demo/viewer.cpp)
See also pointSize(), QFontInfo and setPixelSize().
Example: fonts/simple-qfont-demo/viewer.cpp.
See also pointSizeFloat(), setPointSize() and setPixelSize().
Calling this function only has effect under the X Window System. 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 choice.
See also rawMode() and setRawName().
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 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().
If enable is TRUE a horizontal line in half character height indicating cancelation is drawn above strings written with this QFont.
See also strikeOut() and QFontInfo.
Without explicit setting of a style hint AnyStyle is used.
The style strategy defaults to PreferDefault. When setting a strategy note that right now, the X version only supports bitmap fonts.
In the example below the push button will display its text label with the NewYork font family if this family is available, if not it will display its text label with another serif font:
QFont font( "Newyork", 18 ); font.setStyleHint( QFont::SansSerif );
greetings->setFont( font );
See also StyleHint, styleHint(), StyleStrategy, styleStrategy() and QFontInfo.
Examples: desktop/desktop.cpp and fonts/simple-qfont-demo/viewer.cpp.
See also underline() and QFontInfo.
Examples: fonts/simple-qfont-demo/viewer.cpp and menu/menu.cpp.
QFont font( "Tokyo" );
font.setWeight( QFont::Bold );
(Code taken from fonts/simple-qfont-demo/viewer.cpp)
Strictly speaking you can use all values in the range [0,99] (where 0 is ultralight and 99 is extremely black), but this 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::weight() to check the actual weight.
See also weight() and QFontInfo.
Example: fonts/simple-qfont-demo/viewer.cpp.
Use QFontInfo to find out whether the window system font actually used draws a horizontal line through strings.
See also setStrikeOut() and QFontInfo::strikeOut().
See also setStyleHint() and QFontInfo::styleHint().
See also setStyleHint().
If there is no substitution for familyName, familyName is returned.
To obtain a list of all substitutions use substitutes().
See also setFamily(), insertSubstitutions(), insertSubstitution() and removeSubstitution().
If there is no substitution for familyName, an empty list is returned.
(Code taken from fonts/simple-qfont-demo/viewer.cpp)
See also substitute(), insertSubstitutions(), insertSubstitution() and removeSubstitution().
Example: fonts/simple-qfont-demo/viewer.cpp.
See also insertSubstitution(), removeSubstitution() and substitute().
See also fromString().
Use QFontInfo to find out whether the window system font actually used for drawing is underlined or not.
See also setUnderline() and QFontInfo::underline().
Use QFontInfo to find the weight of the window system font actually used.
See also setWeight(), Weight and QFontInfo.
Writes the font font to the stream s.
See also Format of the QDataStream operators.
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-2000 Trolltech, all rights reserved.
Copyright © 2000 Trolltech | Trademarks | Qt version main-beta1
|