Skip to content

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