QFontDatabase Class
The QFontDatabase class provides information about the fonts available in the underlying window system. More...
Header: | #include <QFontDatabase> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Gui) target_link_libraries(mytarget PRIVATE Qt6::Gui) |
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 } |
Static Public Members
(since 6.8) void | addApplicationFallbackFontFamily(QChar::Script script, const QString &familyName) |
int | addApplicationFont(const QString &fileName) |
int | addApplicationFontFromData(const QByteArray &fontData) |
(since 6.8) QStringList | applicationFallbackFontFamilies(QChar::Script script) |
QStringList | applicationFontFamilies(int id) |
bool | bold(const QString &family, const QString &style) |
QStringList | families(QFontDatabase::WritingSystem writingSystem = Any) |
QFont | font(const QString &family, const QString &style, int pointSize) |
bool | isBitmapScalable(const QString &family, const QString &style = QString()) |
bool | isFixedPitch(const QString &family, const QString &style = QString()) |
bool | isPrivateFamily(const QString &family) |
bool | isScalable(const QString &family, const QString &style = QString()) |
bool | isSmoothlyScalable(const QString &family, const QString &style = QString()) |
bool | italic(const QString &family, const QString &style) |
QList<int> | pointSizes(const QString &family, const QString &styleName = QString()) |
bool | removeAllApplicationFonts() |
(since 6.8) bool | removeApplicationFallbackFontFamily(QChar::Script script, const QString &familyName) |
bool | removeApplicationFont(int id) |
(since 6.8) void | setApplicationFallbackFontFamilies(QChar::Script script, const QStringList &familyNames) |
QList<int> | smoothSizes(const QString &family, const QString &styleName) |
QList<int> | standardSizes() |
QString | styleString(const QFont &font) |
QString | styleString(const QFontInfo &fontInfo) |
QStringList | styles(const QString &family) |
QFont | systemFont(QFontDatabase::SystemFont type) |
int | weight(const QString &family, const QString &style) |
QString | writingSystemName(QFontDatabase::WritingSystem writingSystem) |
QString | writingSystemSample(QFontDatabase::WritingSystem writingSystem) |
QList<QFontDatabase::WritingSystem> | writingSystems() |
QList<QFontDatabase::WritingSystem> | writingSystems(const QString &family) |
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 provides some helper 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:
QTreeWidget fontTree; fontTree.setColumnCount(2); fontTree.setHeaderLabels(QStringList() << "Font" << "Smooth Sizes"); const QStringList fontFamilies = QFontDatabase::families(); for (const QString &family : fontFamilies) { QTreeWidgetItem *familyItem = new QTreeWidgetItem(&fontTree); familyItem->setText(0, family); const QStringList fontStyles = QFontDatabase::styles(family); for (const QString &style : fontStyles) { QTreeWidgetItem *styleItem = new QTreeWidgetItem(familyItem); styleItem->setText(0, style); QString sizes; const QList<int> smoothSizes = QFontDatabase::smoothSizes(family, style); for (const auto &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, and QFontMetrics.
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. |
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 |
Member Function Documentation
[static, since 6.8]
void QFontDatabase::addApplicationFallbackFontFamily(QChar::Script script, const QString &familyName)
Adds familyName as an application-defined fallback font for script.
When Qt encounters characters that are not supported by the selected font, it will search through a list of fallback fonts to find a match for them. This ensures that combining multiple scripts in a single string is possible, even if the main font does not support them.
The list of fallback fonts is selected based on the script of the string as well as other conditions, such as system language.
While the system fallback list is usually sufficient, there are cases where it is useful to override the default behavior. One such case is for using application fonts as fallback to ensure cross-platform consistency.
In another case the application may be written in a script with regional differences and want to run it untranslated in multiple regions. In this case, it might be useful to override the local region's fallback with one that matches the language of the application.
By passing familyName to addApplicationFallbackFontFamily(), this will become the preferred family when matching missing characters from script. The script must be a valid script (QChar::Script_Latin
or higher). When adding multiple fonts for the same script, they will be prioritized in reverse order, so that the last family added will be checked first and so on.
Note: Qt's font matching algorithm considers QChar::Script_Common
(undetermined script) and QChar::Script_Latin
the same. Adding a fallback for either of these will also apply to the other.
This function was introduced in Qt 6.8.
See also setApplicationFallbackFontFamilies(), removeApplicationFallbackFontFamily(), and applicationFallbackFontFamilies().
[static]
int QFontDatabase::addApplicationFont(const QString &fileName)
Loads the font from the file specified by fileName and makes it available to the application. An ID is returned that can be used to remove the font again with removeApplicationFont() or to retrieve the list of family names contained in the font.
The function returns -1 if the font could not be loaded.
Currently only TrueType fonts, TrueType font collections, and OpenType fonts are supported.
See also addApplicationFontFromData(), applicationFontFamilies(), and removeApplicationFont().
[static]
int QFontDatabase::addApplicationFontFromData(const QByteArray &fontData)
Loads the font from binary data specified by fontData and makes it available to the application. An ID is returned that can be used to remove the font again with removeApplicationFont() or to retrieve the list of family names contained in the font.
The function returns -1 if the font could not be loaded.
Currently only TrueType fonts, TrueType font collections, and OpenType fonts are supported.
See also addApplicationFont(), applicationFontFamilies(), and removeApplicationFont().
[static, since 6.8]
QStringList QFontDatabase::applicationFallbackFontFamilies(QChar::Script script)
Returns the list of application-defined fallback font families previously added for script by the addApplicationFallbackFontFamily() function.
This function was introduced in Qt 6.8.
See also setApplicationFallbackFontFamilies(), addApplicationFallbackFontFamily(), and removeApplicationFallbackFontFamily().
[static]
QStringList QFontDatabase::applicationFontFamilies(int id)
Returns a list of font families for the given application font identified by id.
See also addApplicationFont() and addApplicationFontFromData().
[static]
bool QFontDatabase::bold(const QString &family, const QString &style)
Returns true
if the font that has family family and style style is bold; otherwise returns false
.
See also italic() and weight().
[static]
QStringList QFontDatabase::families(QFontDatabase::WritingSystem writingSystem = Any)
Returns a sorted list of the available font families which support the writingSystem.
If a family exists in several foundries, the returned name for that font is in the form "family [foundry]". Examples: "Times [Adobe]", "Times [Cronyx]", "Palatino".
See also writingSystems().
[static]
QFont QFontDatabase::font(const QString &family, const QString &style, int pointSize)
Returns a QFont object that has family family, style style and point size pointSize. If no matching font could be created, a QFont object that uses the application's default font is returned.
[static]
bool QFontDatabase::isBitmapScalable(const QString &family, const QString &style = QString())
Returns true
if the font that has family family and style style is a scalable bitmap font; otherwise returns false
. Scaling a bitmap font usually produces an unattractive hardly readable result, because the pixels of the font are scaled. If you need to scale a bitmap font it is better to scale it to one of the fixed sizes returned by smoothSizes().
See also isScalable() and isSmoothlyScalable().
[static]
bool QFontDatabase::isFixedPitch(const QString &family, const QString &style = QString())
Returns true
if the font that has family family and style style is fixed pitch; otherwise returns false
.
[static]
bool QFontDatabase::isPrivateFamily(const QString &family)
Returns true
if and only if the family font family is private.
This happens, for instance, on macOS and iOS, where the system UI fonts are not accessible to the user. For completeness, QFontDatabase::families() returns all font families, including the private ones. You should use this function if you are developing a font selection control in order to keep private fonts hidden.
See also families().
[static]
bool QFontDatabase::isScalable(const QString &family, const QString &style = QString())
Returns true
if the font that has family family and style style is scalable; otherwise returns false
.
See also isBitmapScalable() and isSmoothlyScalable().
[static]
bool QFontDatabase::isSmoothlyScalable(const QString &family, const QString &style = QString())
Returns true
if the font that has family family and style style is smoothly scalable; otherwise returns false
. If this function returns true
, it's safe to scale this font to any size, and the result will always look attractive.
See also isScalable() and isBitmapScalable().
[static]
bool QFontDatabase::italic(const QString &family, const QString &style)
Returns true
if the font that has family family and style style is italic; otherwise returns false
.
[static]
QList<int> QFontDatabase::pointSizes(const QString &family, const QString &styleName = QString())
Returns a list of the point sizes available for the font that has family family and style styleName. The list may be empty.
See also smoothSizes() and standardSizes().
[static]
bool QFontDatabase::removeAllApplicationFonts()
Removes all application-local fonts previously added using addApplicationFont() and addApplicationFontFromData().
Returns true
if unloading of the fonts succeeded; otherwise returns false
.
See also removeApplicationFont(), addApplicationFont(), and addApplicationFontFromData().
[static, since 6.8]
bool QFontDatabase::removeApplicationFallbackFontFamily(QChar::Script script, const QString &familyName)
Removes familyName from the list of application-defined fallback fonts for script, provided that it has previously been added with addApplicationFallbackFontFamily().
Returns true if the family name was in the list and false if it was not.
This function was introduced in Qt 6.8.
See also addApplicationFallbackFontFamily(), setApplicationFallbackFontFamilies(), and applicationFallbackFontFamilies().
[static]
bool QFontDatabase::removeApplicationFont(int id)
Removes the previously loaded application font identified by id. Returns true
if unloading of the font succeeded; otherwise returns false
.
See also removeAllApplicationFonts(), addApplicationFont(), and addApplicationFontFromData().
[static, since 6.8]
void QFontDatabase::setApplicationFallbackFontFamilies(QChar::Script script, const QStringList &familyNames)
Sets the list of application-defined fallback fonts for script to familyNames.
When Qt encounters a character in script which is not supported by the current font, it will check the families in familyNames, in order from first to last, until it finds a match. See addApplicationFallbackFontFamily() for more details.
This function overwrites the current list of application-defined fallback fonts for script.
This function was introduced in Qt 6.8.
See also addApplicationFallbackFontFamily(), removeApplicationFallbackFontFamily(), and applicationFallbackFontFamilies().
[static]
QList<int> QFontDatabase::smoothSizes(const QString &family, const QString &styleName)
Returns the point sizes of a font that has family family and style styleName that will look attractive. The list may be empty. For non-scalable fonts and bitmap scalable fonts, this function is equivalent to pointSizes().
See also pointSizes() and standardSizes().
[static]
QList<int> QFontDatabase::standardSizes()
Returns a list of standard font sizes.
See also smoothSizes() and pointSizes().
[static]
QString QFontDatabase::styleString(const QFont &font)
Returns a string that describes the style of the font. For example, "Bold Italic", "Bold", "Italic" or "Normal". An empty string may be returned.
[static]
QString QFontDatabase::styleString(const QFontInfo &fontInfo)
Returns a string that describes the style of the fontInfo. For example, "Bold Italic", "Bold", "Italic" or "Normal". An empty string may be returned.
[static]
QStringList QFontDatabase::styles(const QString &family)
Returns a list of the styles available for the font family family. Some example styles: "Light", "Light Italic", "Bold", "Oblique", "Demi". The list may be empty.
See also families().
[static]
QFont QFontDatabase::systemFont(QFontDatabase::SystemFont type)
Returns the most adequate font for a given type case for proper integration with the system's look and feel.
See also QGuiApplication::font().
[static]
int QFontDatabase::weight(const QString &family, const QString &style)
Returns the weight of the font that has family family and style style. If there is no such family and style combination, returns -1.
[static]
QString QFontDatabase::writingSystemName(QFontDatabase::WritingSystem writingSystem)
Returns the names the writingSystem (e.g. for displaying to the user in a dialog).
[static]
QString QFontDatabase::writingSystemSample(QFontDatabase::WritingSystem writingSystem)
Returns a string with sample characters from writingSystem.
[static]
QList<QFontDatabase::WritingSystem> QFontDatabase::writingSystems()
Returns a sorted list of the available writing systems. This is list generated from information about all installed fonts on the system.
See also families().
[static]
QList<QFontDatabase::WritingSystem> QFontDatabase::writingSystems(const QString &family)
Returns a sorted list of the writing systems supported by a given font family.
See also families().
© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.