QFontDatabase Class

The QFontDatabase class provides information about the fonts available in the underlying window system. More...

Header: #include <QFontDatabase>
qmake: QT += gui

Note: All functions in this class are thread-safe.

Public Types

enum SystemFont { GeneralFont, FixedFont, TitleFont, SmallestReadableFont }
enum WritingSystem { Any, Latin, Greek, Cyrillic, Armenian, …, Nko }

Detailed Description

The most common uses of this class are to query the database for the list of font families() and for the pointSizes() and styles() that are available for each family. An alternative to pointSizes() is smoothSizes() which returns the sizes at which a given family and style will look attractive.

If the font family is available from two or more foundries the foundry name is included in the family name; for example: "Helvetica [Adobe]" and "Helvetica [Cronyx]". When you specify a family, you can either use the old hyphenated "foundry-family" format or the bracketed "family [foundry]" format; for example: "Cronyx-Helvetica" or "Helvetica [Cronyx]". If the family has a foundry it is always returned using the bracketed format, as is the case with the value returned by families().

The font() function returns a QFont given a family, style and point size.

A family and style combination can be checked to see if it is italic() or bold(), and to retrieve its weight(). Similarly we can call isBitmapScalable(), isSmoothlyScalable(), isScalable() and isFixedPitch().

Use the styleString() to obtain a text version of a style.

The QFontDatabase class also supports some static functions, for example, standardSizes(). You can retrieve the description of a writing system using writingSystemName(), and a sample of characters in a writing system with writingSystemSample().

Example:

     QFontDatabase database;
     QTreeWidget fontTree;
     fontTree.setColumnCount(2);
     fontTree.setHeaderLabels(QStringList() << "Font" << "Smooth Sizes");

     const QStringList fontFamilies = database.families();
     for (const QString &family : fontFamilies) {
         QTreeWidgetItem *familyItem = new QTreeWidgetItem(&fontTree);
         familyItem->setText(0, family);

         const QStringList fontStyles = database.styles(family);
         for (const QString &style : fontStyles) {
             QTreeWidgetItem *styleItem = new QTreeWidgetItem(familyItem);
             styleItem->setText(0, style);

             QString sizes;
             const QList<int> smoothSizes = database.smoothSizes(family, style)
             for (int points : smoothSizes)
                 sizes += QString::number(points) + ' ';

             styleItem->setText(1, sizes.trimmed());
         }
     }

This example gets the list of font families, the list of styles for each family, and the point sizes that are available for each combination of family and style, displaying this information in a tree view.

See also QFont, QFontInfo, QFontMetrics, and Character Map Example.

Member Type Documentation

enum QFontDatabase::SystemFont

ConstantValueDescription
QFontDatabase::GeneralFont0The default system font.
QFontDatabase::FixedFont1The fixed font that the system recommends.
QFontDatabase::TitleFont2The system standard font for titles.
QFontDatabase::SmallestReadableFont3The smallest readable system font.

This enum was introduced or modified in Qt 5.2.

enum QFontDatabase::WritingSystem

ConstantValueDescription
QFontDatabase::Any0 
QFontDatabase::Latin1 
QFontDatabase::Greek2 
QFontDatabase::Cyrillic3 
QFontDatabase::Armenian4 
QFontDatabase::Hebrew5 
QFontDatabase::Arabic6 
QFontDatabase::Syriac7 
QFontDatabase::Thaana8 
QFontDatabase::Devanagari9 
QFontDatabase::Bengali10 
QFontDatabase::Gurmukhi11 
QFontDatabase::Gujarati12 
QFontDatabase::Oriya13 
QFontDatabase::Tamil14 
QFontDatabase::Telugu15 
QFontDatabase::Kannada16 
QFontDatabase::Malayalam17 
QFontDatabase::Sinhala18 
QFontDatabase::Thai19 
QFontDatabase::Lao20 
QFontDatabase::Tibetan21 
QFontDatabase::Myanmar22 
QFontDatabase::Georgian23 
QFontDatabase::Khmer24 
QFontDatabase::SimplifiedChinese25 
QFontDatabase::TraditionalChinese26 
QFontDatabase::Japanese27 
QFontDatabase::Korean28 
QFontDatabase::Vietnamese29 
QFontDatabase::Symbol30 
QFontDatabase::OtherSymbol(the same as Symbol)
QFontDatabase::Ogham31 
QFontDatabase::Runic32 
QFontDatabase::Nko33