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
| Constant | Value | Description |
|---|---|---|
QFontDatabase::GeneralFont | 0 | The default system font. |
QFontDatabase::FixedFont | 1 | The fixed font that the system recommends. |
QFontDatabase::TitleFont | 2 | The system standard font for titles. |
QFontDatabase::SmallestReadableFont | 3 | The smallest readable system font. |
This enum was introduced or modified in Qt 5.2.
enum QFontDatabase::WritingSystem
| Constant | Value | Description |
|---|---|---|
QFontDatabase::Any | 0 | |
QFontDatabase::Latin | 1 | |
QFontDatabase::Greek | 2 | |
QFontDatabase::Cyrillic | 3 | |
QFontDatabase::Armenian | 4 | |
QFontDatabase::Hebrew | 5 | |
QFontDatabase::Arabic | 6 | |
QFontDatabase::Syriac | 7 | |
QFontDatabase::Thaana | 8 | |
QFontDatabase::Devanagari | 9 | |
QFontDatabase::Bengali | 10 | |
QFontDatabase::Gurmukhi | 11 | |
QFontDatabase::Gujarati | 12 | |
QFontDatabase::Oriya | 13 | |
QFontDatabase::Tamil | 14 | |
QFontDatabase::Telugu | 15 | |
QFontDatabase::Kannada | 16 | |
QFontDatabase::Malayalam | 17 | |
QFontDatabase::Sinhala | 18 | |
QFontDatabase::Thai | 19 | |
QFontDatabase::Lao | 20 | |
QFontDatabase::Tibetan | 21 | |
QFontDatabase::Myanmar | 22 | |
QFontDatabase::Georgian | 23 | |
QFontDatabase::Khmer | 24 | |
QFontDatabase::SimplifiedChinese | 25 | |
QFontDatabase::TraditionalChinese | 26 | |
QFontDatabase::Japanese | 27 | |
QFontDatabase::Korean | 28 | |
QFontDatabase::Vietnamese | 29 | |
QFontDatabase::Symbol | 30 | |
QFontDatabase::Other | Symbol | (the same as Symbol) |
QFontDatabase::Ogham | 31 | |
QFontDatabase::Runic | 32 | |
QFontDatabase::Nko | 33 |