![]() |
| ||
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.
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. If you want to find out font metrics use a QFontMetrics object.
Use QApplication::setFont() to set the application's default font.
If a choosen X11 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 a QFont like this:
QFont serifFont( "Times", 10, Bold );
The attributes set in the constructor can also be set later, e.g. setFamily(), setPointSize(), setWeight() and setItalic(). The remaining attributes must be set after contstruction, e.g. setBold(), setUnderline(), 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().
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 the X Window System 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 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: 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(), QWidget::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-2001 Trolltech, all rights reserved.
Copyright © 2001 Trolltech | Trademarks | Qt version 3.0.0-beta1-beta1
|