Skip to content

PythonQt.Qt.QFontMetrics

QFontMetrics

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

Detailed Description

QFontMetrics functions calculate the size of characters and strings for a given font. There are three ways you can create a QFontMetrics object:

  1. Calling the QFontMetrics constructor with a QFont creates a font metrics object for a screen-compatible font, i.e. the font cannot be a printer font. If the font is changed later, the font metrics object is not updated.

(Note: If you use a printer font the values returned may be inaccurate. Printer fonts are not always accessible so the nearest screen font is used if a printer font is supplied.) 2. QWidget::fontMetrics() returns the font metrics for a widget's font. This is equivalent to QFontMetrics(widget->font()). If the widget's font is changed later, the font metrics object is not updated. 3. QPainter::fontMetrics() returns the font metrics for a painter's current font. If the painter's font is changed later, the font metrics object is not updated.

Once created, the object provides functions to access the individual metrics of the font, its characters, and for strings rendered in the font.

There are several functions that operate on the font: ascent(), descent(), height(), leading() and lineSpacing() return the basic size properties of the font. The underlinePos(), overlinePos(), strikeOutPos() and lineWidth() functions, return the properties of the line that underlines, overlines or strikes out the characters. These functions are all fast.

There are also some functions that operate on the set of glyphs in the font: minLeftBearing(), minRightBearing() and maxWidth(). These are by necessity slow, and we recommend avoiding them if possible.

For each character, you can get its horizontalAdvance(), leftBearing(), and rightBearing(), and find out whether it is in the font using inFont(). You can also treat the character as a string, and use the string functions on it.

The string functions include horizontalAdvance(), to return the width of a string in pixels (or points, for a printer), boundingRect(), to return a rectangle large enough to contain the rendered string, and size(), to return the size of that rectangle.

Example:

QFont font("times", 24);
QFontMetrics fm(font);
int pixelsWide = fm.horizontalAdvance("What's the width of this text?");
int pixelsHigh = fm.height();

Source: https://doc.qt.io/archives/qt-5.15/qfontmetrics.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
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
ascent function (self) -> int X.ascent() -> int (shared captured text)
averageCharWidth function (self) -> int X.averageCharWidth() -> int (shared captured text)
boundingRect function (self, x: Any, y: Any, w: Any, h: Any, arg4: flags, text: str, tabstops: Any, tabarray: Any) -> Any X.boundingRect(x, y, w, h, flags, text, tabstops, tabarray) -> PythonQt.QtCore.QRect (shared captured text)
charWidth function (self, str: str, pos: int) -> int X.charWidth(str, pos) -> int (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
descent function (self) -> int X.descent() -> int (shared captured text)
elidedText function (self, text: str, mode: Any, width: int, arg3: flags) -> str X.elidedText(text, mode, width, flags) -> str (shared captured text)
height function (self) -> int X.height() -> int (shared captured text)
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
inFont function (self, arg__1: str) -> bool X.inFont(arg__1) -> bool (shared captured text)
inFontUcs4 function (self, ucs4: int) -> bool X.inFontUcs4(ucs4) -> bool (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)
leading function (self) -> int X.leading() -> int (shared captured text)
leftBearing function (self, arg__1: str) -> int X.leftBearing(arg__1) -> int (shared captured text)
lineSpacing function (self) -> int X.lineSpacing() -> int (shared captured text)
lineWidth function (self) -> int X.lineWidth() -> int (shared captured text)
maxWidth function (self) -> int X.maxWidth() -> int (shared captured text)
minLeftBearing function (self) -> int X.minLeftBearing() -> int (shared captured text)
minRightBearing function (self) -> int X.minRightBearing() -> int (shared captured text)
overlinePos function (self) -> int X.overlinePos() -> int (shared captured text)
rightBearing function (self, arg__1: str) -> int X.rightBearing(arg__1) -> int (shared captured text)
size function (self, arg0: flags, str: str, tabstops: int, tabarray: Any) -> Any X.size(flags, str, tabstops, tabarray) -> PythonQt.QtCore.QSize (shared captured text)
strikeOutPos function (self) -> int X.strikeOutPos() -> int (shared captured text)
swap function (self, other: Any) -> None Swaps this font metrics instance with other. (shared captured text)
tightBoundingRect function (self, text: str) -> Any X.tightBoundingRect(text) -> PythonQt.QtCore.QRect (shared captured text)
underlinePos function (self) -> int X.underlinePos() -> int (shared captured text)
width function (self, arg__1: str, len: int, arg2: flags) -> int X.width(arg__1, len, flags) -> int (shared captured text)
xHeight function (self) -> int X.xHeight() -> int (shared captured text)