Skip to content

PythonQt.QtGui.QTextLayout

QTextLayout

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

Detailed Description

It offers many features expected from a modern text layout engine, including Unicode compliant rendering, line breaking and handling of cursor positioning. It can also produce and render device independent layout, something that is important for WYSIWYG applications.

The class has a rather low level API and unless you intend to implement your own text rendering for some specialized widget, you probably won't need to use it directly.

QTextLayout can be used with both plain and rich text.

QTextLayout can be used to create a sequence of QTextLine instances with given widths and can position them independently on the screen. Once the layout is done, these lines can be drawn on a paint device.

The text to be laid out can be provided in the constructor or set with setText().

The layout can be seen as a sequence of QTextLine objects; use createLine() to create a QTextLine instance, and lineAt() or lineForTextPosition() to retrieve created lines.

Here is a code snippet that demonstrates the layout phase:

int leading = fontMetrics.leading();
qreal height = 0;
textLayout.setCacheEnabled(true);
textLayout.beginLayout();
while (1) {
    QTextLine line = textLayout.createLine();
    if (!line.isValid())
        break;

    line.setLineWidth(lineWidth);
    height += leading;
    line.setPosition(QPointF(0, height));
    height += line.height();
}
textLayout.endLayout();

The text can then be rendered by calling the layout's draw() function:

QPainter painter(this);
textLayout.draw(&painter, QPoint(0, 0));

For a given position in the text you can find a valid cursor position with isValidCursorPosition(), nextCursorPosition(), and previousCursorPosition().

The QTextLayout itself can be positioned with setPosition(); it has a boundingRect(), and a minimumWidth() and a maximumWidth().

Source: https://doc.qt.io/archives/qt-5.15/qtextlayout.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
CursorMode attribute type Convert a number or string to an integer, or return 0 if no arguments are given.
FormatRange attribute type
SkipCharacters attribute Any
SkipWords attribute Any
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
additionalFormats function (self) -> tuple X.additionalFormats() -> tuple
beginLayout function (self) -> None Begins the layout process.
boundingRect function (self) -> Any X.boundingRect() -> PythonQt.QtCore.QRectF
cacheEnabled function (self) -> bool X.cacheEnabled() -> bool
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
clearAdditionalFormats function (self) -> None
clearFormats function (self) -> None Clears the list of additional formats supported by the text layout.
clearLayout function (self) -> None Clears the line information in the layout.
createLine function (self) -> Any X.createLine() -> PythonQt.QtGui.QTextLine
cursorMoveStyle function (self) -> Any The cursor movement style of this [QTextLayout](https://doc.qt.io/archives/qt-5.15/qtextlayout.html).
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
draw function (self, p: Any, pos: Any, selections: Any, clip: Any) -> None X.draw(p, pos, selections, clip)
drawCursor function (self, p: Any, pos: Any, cursorPosition: int, width: Any) -> None X.drawCursor(p, pos, cursorPosition, width)
endLayout function (self) -> None Ends the layout process.
font function (self) -> Any X.font() -> PythonQt.QtGui.QFont
formats function (self) -> tuple X.formats() -> tuple
glyphRuns function (self, *args: Any, **kwargs: Any) -> Any X.glyphRuns(from, length) -> tuple
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (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)
isValidCursorPosition function (self, pos: int) -> bool X.isValidCursorPosition(pos) -> bool
leftCursorPosition function (self, oldPos: int) -> int X.leftCursorPosition(oldPos) -> int
lineAt function (self, i: int) -> Any X.lineAt(i) -> PythonQt.QtGui.QTextLine
lineCount function (self) -> int X.lineCount() -> int
lineForTextPosition function (self, pos: int) -> Any X.lineForTextPosition(pos) -> PythonQt.QtGui.QTextLine
maximumWidth function (self) -> float X.maximumWidth() -> float
minimumWidth function (self) -> float X.minimumWidth() -> float
nextCursorPosition function (self, oldPos: int, mode: Any) -> int X.nextCursorPosition(oldPos, mode) -> int
position function (self) -> Any X.position() -> PythonQt.QtCore.QPointF
preeditAreaPosition function (self) -> int X.preeditAreaPosition() -> int
preeditAreaText function (self) -> str X.preeditAreaText() -> str
previousCursorPosition function (self, oldPos: int, mode: Any) -> int X.previousCursorPosition(oldPos, mode) -> int
rightCursorPosition function (self, oldPos: int) -> int X.rightCursorPosition(oldPos) -> int
setAdditionalFormats function (self, overrides: Any) -> None
setCacheEnabled function (self, enable: bool) -> None Enables caching of the complete layout information if enable is true; otherwise disables layout caching.
setCursorMoveStyle function (self, style: Any) -> None Sets the visual cursor movement style to the given style.
setFlags function (self, arg0: flags) -> None
setFont function (self, f: Any) -> None Sets the layout's font to the given font.
setFormats function (self, overrides: Any) -> None Sets the additional formats supported by the text layout to formats.
setPosition function (self, p: Any) -> None Moves the text layout to point p.
setPreeditArea function (self, position: int, text: str) -> None X.setPreeditArea(position, text)
setRawFont function (self, rawFont: Any) -> None
setText function (self, string: str) -> None Sets the layout's text to the given string.
setTextOption function (self, option: Any) -> None Sets the text option structure that controls the layout process to the given option.
text function (self) -> str X.text() -> str
textOption function (self) -> Any X.textOption() -> PythonQt.QtGui.QTextOption