Skip to content

PythonQt.Qt.QFont

QFont

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

Detailed Description

QFont can be regarded as a query for one or more fonts on the system.

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 QFontMetricsF to get measurements, e.g. the pixel length of a string using QFontMetrics::width().

Attributes which are not specifically set will not affect the font selection algorithm, and default values will be preferred instead.

To load a specific physical font, typically represented by a single file, use QRawFont instead.

Note that a QGuiApplication instance must exist before a QFont can be used. You can set the application's default font with QGuiApplication::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:

QFont serifFont("Times", 10, QFont::Bold);
QFont sansFont("Helvetica [Cronyx]", 12);

The attributes set in the constructor can also be set later, e.g. setFamily(), setPointSize(), setPointSizeF(), 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().

You can provide substitutions for font family names using insertSubstitution() and insertSubstitutions(). Substitutions can be removed with removeSubstitutions(). 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). After substituting a font, you must trigger the updating of the font by destroying and re-creating all QFont objects.

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.

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.

The font matching algorithm works as follows:

  1. The specified font families (set by setFamilies()) are searched for.
  2. If not found, then if set the specified font family exists and can be used to represent the writing system in use, it will be selected.
  3. If not, a replacement font that supports the writing system is selected. The font matching algorithm will try to find the best match for all the properties set in the QFont. How this is done varies from platform to platform.
  4. If no font exists on the system that can support the text, then special "missing character" boxes will be shown in its place.

Note: If the selected font, though supporting the writing system in general, is missing glyphs for one or more specific characters, then Qt will try to find a fallback font for this or these particular characters. This feature can be disabled using QFont::NoFontMerging style strategy.

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()
  2. pointSize() (see below)
  3. weight()
  4. 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.

Examples:

QFont f("Helvetica");

If you had both an Adobe and a Cronyx Helvetica, you might get either.

QFont f("Helvetica [Cronyx]");

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.

QFontInfo info(f1);
QString family = info.family();

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

QFontMetrics fm(f1);
int textWidthInPixels = fm.horizontalAdvance("How many pixels wide is this text?");
int textHeightInPixels = fm.height();

For more general information on fonts, see the comp.fonts FAQ. Information on encodings can be found from Roman Czyborra's page.

Source: https://doc.qt.io/archives/qt-5.15/qfont.html

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
AbsoluteSpacing attribute Any (shared captured text)
AllLowercase attribute Any (shared captured text)
AllUppercase attribute Any (shared captured text)
AnyStyle attribute Any (shared captured text)
Black attribute Any (shared captured text)
Bold attribute Any (shared captured text)
Capitalization attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
Capitalize attribute Any (shared captured text)
Condensed attribute Any (shared captured text)
Courier attribute Any (shared captured text)
Cursive attribute Any (shared captured text)
Decorative attribute Any (shared captured text)
DemiBold attribute Any (shared captured text)
Expanded attribute Any (shared captured text)
ExtraBold attribute Any (shared captured text)
ExtraCondensed attribute Any (shared captured text)
ExtraExpanded attribute Any (shared captured text)
ExtraLight attribute Any (shared captured text)
Fantasy attribute Any (shared captured text)
ForceIntegerMetrics attribute Any (shared captured text)
ForceOutline attribute Any (shared captured text)
Helvetica attribute Any (shared captured text)
HintingPreference attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
Light attribute Any (shared captured text)
Medium attribute Any (shared captured text)
MixedCase attribute Any (shared captured text)
Monospace attribute Any (shared captured text)
NoAntialias attribute Any (shared captured text)
NoFontMerging attribute Any (shared captured text)
NoSubpixelAntialias attribute Any (shared captured text)
Normal attribute Any (shared captured text)
OldEnglish attribute Any (shared captured text)
OpenGLCompatible attribute Any (shared captured text)
PercentageSpacing attribute Any (shared captured text)
PreferAntialias attribute Any (shared captured text)
PreferBitmap attribute Any (shared captured text)
PreferDefault attribute Any (shared captured text)
PreferDefaultHinting attribute Any (shared captured text)
PreferDevice attribute Any (shared captured text)
PreferFullHinting attribute Any (shared captured text)
PreferMatch attribute Any (shared captured text)
PreferNoHinting attribute Any (shared captured text)
PreferOutline attribute Any (shared captured text)
PreferQuality attribute Any (shared captured text)
PreferVerticalHinting attribute Any (shared captured text)
SansSerif attribute Any (shared captured text)
SemiCondensed attribute Any (shared captured text)
SemiExpanded attribute Any (shared captured text)
Serif attribute Any (shared captured text)
SmallCaps attribute Any (shared captured text)
SpacingType attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
Stretch attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
Style attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
StyleHint attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
StyleItalic attribute Any (shared captured text)
StyleNormal attribute Any (shared captured text)
StyleOblique attribute Any (shared captured text)
StyleStrategy attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
System attribute Any (shared captured text)
Thin attribute Any (shared captured text)
Times attribute Any (shared captured text)
TypeWriter attribute Any (shared captured text)
UltraCondensed attribute Any (shared captured text)
UltraExpanded attribute Any (shared captured text)
Unstretched attribute Any (shared captured text)
Weight attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
bold function (self) -> bool X.bold() -> bool (shared captured text)
cacheStatistics function (self) -> None (shared captured text)
capitalization function (self) -> Any Returns the current capitalization type of the font. (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
cleanup function (self) -> None (shared captured text)
defaultFamily function (self) -> str X.defaultFamily() -> str (shared captured text)
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
exactMatch function (self) -> bool X.exactMatch() -> bool (shared captured text)
family function (self) -> str X.family() -> str (shared captured text)
fixedPitch function (self) -> bool X.fixedPitch() -> bool (shared captured text)
fromString function (self, arg__1: str) -> bool X.fromString(arg__1) -> bool (shared captured text)
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
hintingPreference function (self) -> Any Returns the currently preferred hinting level for glyphs rendered with this font. (shared captured text)
inherits function (self, *args: Any, **kwargs: Any) -> Any Returns if the class inherits or is of given type name (shared captured text)
initialize function (self) -> None (shared captured text)
insertSubstitution function (self, arg__1: str, arg__2: str) -> None X.insertSubstitution(arg__1, arg__2) (shared captured text)
insertSubstitutions function (self, arg__1: str, arg__2: Any) -> None X.insertSubstitutions(arg__1, arg__2) (shared captured text)
isCopyOf function (self, arg__1: Any) -> bool X.isCopyOf(arg__1) -> bool (shared captured text)
italic function (self) -> bool X.italic() -> bool (shared captured text)
kerning function (self) -> bool X.kerning() -> bool (shared captured text)
key function (self) -> str X.key() -> str (shared captured text)
lastResortFamily function (self) -> str X.lastResortFamily() -> str (shared captured text)
lastResortFont function (self) -> str X.lastResortFont() -> str (shared captured text)
letterSpacing function (self) -> float X.letterSpacing() -> float (shared captured text)
letterSpacingType function (self) -> Any Returns the spacing type used for letter spacing. (shared captured text)
overline function (self) -> bool X.overline() -> bool (shared captured text)
pixelSize function (self) -> int X.pixelSize() -> int (shared captured text)
pointSize function (self) -> int X.pointSize() -> int (shared captured text)
pointSizeF function (self) -> float X.pointSizeF() -> float (shared captured text)
rawMode function (self) -> bool X.rawMode() -> bool (shared captured text)
rawName function (self) -> str X.rawName() -> str (shared captured text)
readFrom function (self, arg__1: Any) -> Any (shared captured text)
removeSubstitutions function (self, arg__1: str) -> None Removes all the substitutions for familyName. (shared captured text)
resolve function (self, arg__1: Any) -> int X.resolve(arg__1) -> int (shared captured text)
setBold function (self, arg__1: bool) -> None If enable is true sets the font's weight to [QFont::Bold](https://doc.qt.io/archives/qt-5.15/qfont.html#Weight-enum); otherwise sets the weight to… (shared captured text)
setCapitalization function (self, arg__1: Any) -> None Sets the capitalization of the text in this font to caps. (shared captured text)
setFamily function (self, arg__1: str) -> None Sets the family name of the font. (shared captured text)
setFixedPitch function (self, arg__1: bool) -> None If enable is true, sets fixed pitch on; otherwise sets fixed pitch off. (shared captured text)
setHintingPreference function (self, hintingPreference: Any) -> None Set the preference for the hinting level of the glyphs to hintingPreference. (shared captured text)
setItalic function (self, b: bool) -> None Sets the [style](https://doc.qt.io/archives/qt-5.15/qfont.html#style)() of the font to [QFont::StyleItalic](https://doc.qt.io/archives/qt-5.15/qfont.html#Style-enum) if enable is true; otherwise… (shared captured text)
setKerning function (self, arg__1: bool) -> None Enables kerning for this font if enable is true; otherwise disables it. (shared captured text)
setLetterSpacing function (self, type: Any, spacing: float) -> None X.setLetterSpacing(type, spacing) (shared captured text)
setOverline function (self, arg__1: bool) -> None If enable is true, sets overline on; otherwise sets overline off. (shared captured text)
setPixelSize function (self, arg__1: int) -> None Sets the font size to pixelSize pixels. (shared captured text)
setPointSize function (self, arg__1: int) -> None Sets the point size to pointSize. (shared captured text)
setPointSizeF function (self, arg__1: float) -> None Sets the point size to pointSize. (shared captured text)
setRawMode function (self, arg__1: bool) -> None (shared captured text)
setRawName function (self, arg__1: str) -> None (shared captured text)
setStretch function (self, arg__1: int) -> None Sets the stretch factor for the font. (shared captured text)
setStrikeOut function (self, arg__1: bool) -> None If enable is true, sets strikeout on; otherwise sets strikeout off. (shared captured text)
setStyle function (self, style: Any) -> None Sets the style of the font to style. (shared captured text)
setStyleHint function (self, arg__1: Any, arg__2: Any) -> None X.setStyleHint(arg__1, arg__2) (shared captured text)
setStyleName function (self, arg__1: str) -> None Sets the style name of the font to styleName. (shared captured text)
setStyleStrategy function (self, s: Any) -> None Sets the style strategy for the font to s. (shared captured text)
setUnderline function (self, arg__1: bool) -> None If enable is true, sets underline on; otherwise sets underline off. (shared captured text)
setWeight function (self, arg__1: int) -> None Sets the weight of the font to weight, using the scale defined by [QFont::Weight](https://doc.qt.io/archives/qt-5.15/qfont.html#Weight-enum) enumeration. (shared captured text)
setWordSpacing function (self, spacing: float) -> None Sets the word spacing for the font to spacing. (shared captured text)
stretch function (self) -> int X.stretch() -> int (shared captured text)
strikeOut function (self) -> bool X.strikeOut() -> bool (shared captured text)
style function (self) -> Any Returns the style of the font. (shared captured text)
styleHint function (self) -> Any Returns the [StyleHint](https://doc.qt.io/archives/qt-5.15/qfont.html#StyleHint-enum). (shared captured text)
styleName function (self) -> str X.styleName() -> str (shared captured text)
styleStrategy function (self) -> Any Returns the [StyleStrategy](https://doc.qt.io/archives/qt-5.15/qfont.html#StyleStrategy-enum). (shared captured text)
substitute function (self, arg__1: str) -> str X.substitute(arg__1) -> str (shared captured text)
substitutes function (self, arg__1: str) -> tuple X.substitutes(arg__1) -> tuple (shared captured text)
substitutions function (self) -> tuple X.substitutions() -> tuple (shared captured text)
swap function (self, other: Any) -> None Swaps this font instance with other. (shared captured text)
toString function (self) -> str X.toString() -> str (shared captured text)
underline function (self) -> bool X.underline() -> bool (shared captured text)
weight function (self) -> int X.weight() -> int (shared captured text)
wordSpacing function (self) -> float X.wordSpacing() -> float (shared captured text)
writeTo function (self, arg__1: Any) -> Any (shared captured text)