Logo Search packages:      
Sourcecode: qt4-x11 version File versions

QFont Class Reference

List of all members.

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().

Note that a QApplication instance must exist before a QFont can be used. You can set the application's default font with QApplication::setFont().

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:

doc/src/snippets/code/src_gui_text_qfont.cpp 0

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.

In 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.

fontmatching The font matching algorithm works as follows: 1 The specified font family is searched for. If not found, the styleHint() is used to select a replacement family. Each replacement font family is searched for. If none of these are found or there was no styleHint(), "helvetica" will be searched for. If "helvetica" isn't found Qt will try the lastResortFamily(). 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.

In Windows a request for the "Courier" font is automatically changed to "Courier New", an improved version of Courier that allows for smooth scaling. The older "Courier" bitmap font can be selected by setting the PreferBitmap style strategy (see setStyleStrategy()).

Once a font is found, the remaining attributes are matched in order of priority: 1 fixedPitch() pointSize() (see below) weight() 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.


doc/src/snippets/code/src_gui_text_qfont.cpp 1 If you had both an Adobe and a Cronyx Helvetica, you might get either.

doc/src/snippets/code/src_gui_text_qfont.cpp 2

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.

doc/src/snippets/code/src_gui_text_qfont.cpp 3

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

doc/src/snippets/code/src_gui_text_qfont.cpp 4

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, {Character Map Example}

Definition at line 65 of file qfont.h.

Public Types

enum  Capitalization {
  MixedCase, AllUppercase, AllLowercase, SmallCaps,
enum  ResolveProperties {
  FamilyResolved = 0x0001, SizeResolved = 0x0002, StyleHintResolved = 0x0004, StyleStrategyResolved = 0x0008,
  WeightResolved = 0x0010, StyleResolved = 0x0020, UnderlineResolved = 0x0040, OverlineResolved = 0x0080,
  StrikeOutResolved = 0x0100, FixedPitchResolved = 0x0200, StretchResolved = 0x0400, KerningResolved = 0x0800,
  CapitalizationResolved = 0x1000, LetterSpacingResolved = 0x2000, WordSpacingResolved = 0x4000, AllPropertiesResolved = 0x7fff
enum  SpacingType { PercentageSpacing, AbsoluteSpacing }
enum  Stretch {
  UltraCondensed = 50, ExtraCondensed = 62, Condensed = 75, SemiCondensed = 87,
  Unstretched = 100, SemiExpanded = 112, Expanded = 125, ExtraExpanded = 150,
  UltraExpanded = 200
enum  Style { StyleNormal, StyleItalic, StyleOblique }
enum  StyleHint {
  Helvetica, SansSerif = Helvetica, Times, Serif = Times,
  Courier, TypeWriter = Courier, OldEnglish, Decorative = OldEnglish,
  System, AnyStyle
enum  StyleStrategy {
  PreferDefault = 0x0001, PreferBitmap = 0x0002, PreferDevice = 0x0004, PreferOutline = 0x0008,
  ForceOutline = 0x0010, PreferMatch = 0x0020, PreferQuality = 0x0040, PreferAntialias = 0x0080,
  NoAntialias = 0x0100, OpenGLCompatible = 0x0200, NoFontMerging = 0x8000
enum  Weight {
  Light = 25, Normal = 50, DemiBold = 63, Bold = 75,
  Black = 87

Public Member Functions

bool bold () const
Capitalization capitalization () const
QString defaultFamily () const
bool exactMatch () const
QString family () const
bool fixedPitch () const
bool fromString (const QString &)
Qt::HANDLE handle () const
bool isCopyOf (const QFont &) const
bool italic () const
bool kerning () const
QString key () const
QString lastResortFamily () const
QString lastResortFont () const
qreal letterSpacing () const
SpacingType letterSpacingType () const
 operator QVariant () const
bool operator!= (const QFont &) const
bool operator< (const QFont &) const
QFontoperator= (const QFont &)
bool operator== (const QFont &) const
bool overline () const
int pixelSize () const
int pointSize () const
qreal pointSizeF () const
 QFont (const QFont &)
 QFont (const QFont &, QPaintDevice *pd)
 QFont (const QString &family, int pointSize=-1, int weight=-1, bool italic=false)
 QFont ()
bool rawMode () const
QString rawName () const
void resolve (uint mask)
uint resolve () const
QFont resolve (const QFont &) const
void setBold (bool)
void setCapitalization (Capitalization)
void setFamily (const QString &)
void setFixedPitch (bool)
void setItalic (bool b)
void setKerning (bool)
void setLetterSpacing (SpacingType type, qreal spacing)
void setOverline (bool)
void setPixelSize (int)
void setPointSize (int)
void setPointSizeF (qreal)
void setRawMode (bool)
void setRawName (const QString &)
void setStretch (int)
void setStrikeOut (bool)
void setStyle (Style style)
void setStyleHint (StyleHint, StyleStrategy=PreferDefault)
void setStyleStrategy (StyleStrategy s)
void setUnderline (bool)
void setWeight (int)
void setWordSpacing (qreal spacing)
int stretch () const
bool strikeOut () const
Style style () const
StyleHint styleHint () const
StyleStrategy styleStrategy () const
QString toString () const
bool underline () const
int weight () const
qreal wordSpacing () const
 ~QFont ()

Static Public Member Functions

static void cacheStatistics ()
static void cleanup ()
static void initialize ()
static void insertSubstitution (const QString &, const QString &)
static void insertSubstitutions (const QString &, const QStringList &)
static void removeSubstitution (const QString &)
static QString substitute (const QString &)
static QStringList substitutes (const QString &)
static QStringList substitutions ()

Private Member Functions

void detach ()
 QFont (QFontPrivate *)

Private Attributes

QFontPrivate * d
uint resolve_mask


Q_GUI_EXPORT QDataStreamoperator<< (QDataStream &, const QFont &)
Q_GUI_EXPORT QDataStreamoperator>> (QDataStream &, QFont &)
class Q3TextFormatCollection
class QAlphaPaintEngine
class QApplication
class QFontInfo
class QFontMetrics
class QFontMetricsF
class QFontPrivate
class QGLContext
class QPainter
class QPainterPath
class QPicturePaintEngine
class QPSPrintEngineFont
struct QScriptLine
class QStackTextEngine
class QTextEngine
class QTextItemInt
class QTextLayout
class QTextLine
class QWidget
class QWidgetPrivate
class QWin32PaintEngine

The documentation for this class was generated from the following files:

Generated by  Doxygen 1.6.0   Back to index