QTextLayout

The QTextLayout class is used to lay out and render text. More

Inheritance diagram of PySide6.QtGui.QTextLayout

Synopsis

Functions

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:

leading = fontMetrics.leading()
height = 0
textLayout.setCacheEnabled(True)
textLayout.beginLayout()
while 1:
    line = textLayout.createLine()
    if (not 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:

painter = QPainter(self)
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() .

See also

QStaticText

class PySide6.QtGui.QTextLayout

PySide6.QtGui.QTextLayout(text)

PySide6.QtGui.QTextLayout(text, font[, paintdevice=None])

PySide6.QtGui.QTextLayout(b)

Parameters

Constructs an empty text layout.

See also

setText()

Constructs a text layout to lay out the given text.

Constructs a text layout to lay out the given text with the specified font.

All the metric and layout calculations will be done in terms of the paint device, paintdevice. If paintdevice is None the calculations will be done in screen metrics.

Constructs a text layout to lay out the given text.

PySide6.QtGui.QTextLayout.CursorMode

Constant

Description

QTextLayout.SkipCharacters

QTextLayout.SkipWords

PySide6.QtGui.QTextLayout.beginLayout()

Begins the layout process.

Warning

This will invalidate the layout, so all existing QTextLine objects that refer to the previous contents should now be discarded.

See also

endLayout()

PySide6.QtGui.QTextLayout.boundingRect()
Return type

PySide6.QtCore.QRectF

The smallest rectangle that contains all the lines in the layout.

PySide6.QtGui.QTextLayout.cacheEnabled()
Return type

bool

Returns true if the complete layout information is cached; otherwise returns false.

PySide6.QtGui.QTextLayout.clearFormats()

Clears the list of additional formats supported by the text layout.

PySide6.QtGui.QTextLayout.clearLayout()

Clears the line information in the layout. After having called this function, lineCount() returns 0.

Warning

This will invalidate the layout, so all existing QTextLine objects that refer to the previous contents should now be discarded.

PySide6.QtGui.QTextLayout.createLine()
Return type

PySide6.QtGui.QTextLine

Returns a new text line to be laid out if there is text to be inserted into the layout; otherwise returns an invalid text line.

The text layout creates a new line object that starts after the last line in the layout, or at the beginning if the layout is empty. The layout maintains an internal cursor, and each line is filled with text from the cursor position onwards when the setLineWidth() function is called.

Once setLineWidth() is called, a new line can be created and filled with text. Repeating this process will lay out the whole block of text contained in the QTextLayout . If there is no text left to be inserted into the layout, the QTextLine returned will not be valid ( isValid() will return false).

PySide6.QtGui.QTextLayout.cursorMoveStyle()
Return type

CursorMoveStyle

The cursor movement style of this QTextLayout . The default is LogicalMoveStyle .

PySide6.QtGui.QTextLayout.draw(p, pos[, selections=list()[, clip=QRectF()]])
Parameters

Draws the whole layout on the painter p at the position specified by pos. The rendered layout includes the given selections and is clipped within the rectangle specified by clip.

PySide6.QtGui.QTextLayout.drawCursor(p, pos, cursorPosition)
Parameters

This is an overloaded function.

Draws a text cursor with the current pen at the given position using the painter specified. The corresponding position within the text is specified by cursorPosition.

PySide6.QtGui.QTextLayout.drawCursor(p, pos, cursorPosition, width)
Parameters

Draws a text cursor with the current pen and the specified width at the given position using the painter specified. The corresponding position within the text is specified by cursorPosition.

PySide6.QtGui.QTextLayout.endLayout()

Ends the layout process.

See also

beginLayout()

PySide6.QtGui.QTextLayout.font()
Return type

PySide6.QtGui.QFont

Returns the current font that is used for the layout, or a default font if none is set.

See also

setFont()

PySide6.QtGui.QTextLayout.formats()
Return type

Returns the list of additional formats supported by the text layout.

PySide6.QtGui.QTextLayout.glyphRuns([from=-1[, length=-1]])
Parameters
  • from – int

  • length – int

Return type

Returns the glyph indexes and positions for all glyphs corresponding to the length characters starting at the position from in this QTextLayout . This is an expensive function, and should not be called in a time sensitive context.

If from is less than zero, then the glyph run will begin at the first character in the layout. If length is less than zero, it will span the entire string from the start position.

PySide6.QtGui.QTextLayout.isValidCursorPosition(pos)
Parameters

pos – int

Return type

bool

/ Returns true if position pos is a valid cursor position.

In a Unicode context some positions in the text are not valid cursor positions, because the position is inside a Unicode surrogate or a grapheme cluster.

A grapheme cluster is a sequence of two or more Unicode characters that form one indivisible entity on the screen. For example the latin character `Ä’ can be represented in Unicode by two characters, `A’ (0x41), and the combining diaeresis (0x308). A text cursor can only validly be positioned before or after these two characters, never between them since that wouldn’t make sense. In indic languages every syllable forms a grapheme cluster.

PySide6.QtGui.QTextLayout.leftCursorPosition(oldPos)
Parameters

oldPos – int

Return type

int

Returns the cursor position to the left of oldPos, next to it. It’s dependent on the visual position of characters, after bi-directional reordering.

PySide6.QtGui.QTextLayout.lineAt(i)
Parameters

i – int

Return type

PySide6.QtGui.QTextLine

Returns the i-th line of text in this text layout.

PySide6.QtGui.QTextLayout.lineCount()
Return type

int

Returns the number of lines in this text layout.

See also

lineAt()

PySide6.QtGui.QTextLayout.lineForTextPosition(pos)
Parameters

pos – int

Return type

PySide6.QtGui.QTextLine

Returns the line that contains the cursor position specified by pos.

PySide6.QtGui.QTextLayout.maximumWidth()
Return type

float

The maximum width the layout could expand to; this is essentially the width of the entire text.

Warning

This function only returns a valid value after the layout has been done.

See also

minimumWidth()

PySide6.QtGui.QTextLayout.minimumWidth()
Return type

float

The minimum width the layout needs. This is the width of the layout’s smallest non-breakable substring.

Warning

This function only returns a valid value after the layout has been done.

See also

maximumWidth()

PySide6.QtGui.QTextLayout.nextCursorPosition(oldPos[, mode=QTextLayout.CursorMode.SkipCharacters])
Parameters
Return type

int

Returns the next valid cursor position after oldPos that respects the given cursor mode. Returns value of oldPos, if oldPos is not a valid cursor position.

PySide6.QtGui.QTextLayout.position()
Return type

PySide6.QtCore.QPointF

The global position of the layout. This is independent of the bounding rectangle and of the layout process.

See also

setPosition()

PySide6.QtGui.QTextLayout.preeditAreaPosition()
Return type

int

Returns the position of the area in the text layout that will be processed before editing occurs.

PySide6.QtGui.QTextLayout.preeditAreaText()
Return type

str

Returns the text that is inserted in the layout before editing occurs.

PySide6.QtGui.QTextLayout.previousCursorPosition(oldPos[, mode=QTextLayout.CursorMode.SkipCharacters])
Parameters
Return type

int

Returns the first valid cursor position before oldPos that respects the given cursor mode. Returns value of oldPos, if oldPos is not a valid cursor position.

PySide6.QtGui.QTextLayout.rightCursorPosition(oldPos)
Parameters

oldPos – int

Return type

int

Returns the cursor position to the right of oldPos, next to it. It’s dependent on the visual position of characters, after bi-directional reordering.

PySide6.QtGui.QTextLayout.setCacheEnabled(enable)
Parameters

enable – bool

Enables caching of the complete layout information if enable is true; otherwise disables layout caching. Usually QTextLayout throws most of the layouting information away after a call to endLayout() to reduce memory consumption. If you however want to draw the laid out text directly afterwards enabling caching might speed up drawing significantly.

See also

cacheEnabled()

PySide6.QtGui.QTextLayout.setCursorMoveStyle(style)
Parameters

styleCursorMoveStyle

Sets the visual cursor movement style to the given style. If the QTextLayout is backed by a document, you can ignore this and use the option in QTextDocument , this option is for widgets like QLineEdit or custom widgets without a QTextDocument . Default value is LogicalMoveStyle .

PySide6.QtGui.QTextLayout.setFlags(flags)
Parameters

flags – int

PySide6.QtGui.QTextLayout.setFont(f)
Parameters

fPySide6.QtGui.QFont

Sets the layout’s font to the given font. The layout is invalidated and must be laid out again.

See also

font()

PySide6.QtGui.QTextLayout.setFormats(overrides)
Parameters

overrides

Sets the additional formats supported by the text layout to formats. The formats are applied with preedit area text in place.

PySide6.QtGui.QTextLayout.setPosition(p)
Parameters

pPySide6.QtCore.QPointF

Moves the text layout to point p.

See also

position()

PySide6.QtGui.QTextLayout.setPreeditArea(position, text)
Parameters
  • position – int

  • text – str

Sets the position and text of the area in the layout that is processed before editing occurs. The layout is invalidated and must be laid out again.

PySide6.QtGui.QTextLayout.setRawFont(rawFont)
Parameters

rawFontPySide6.QtGui.QRawFont

PySide6.QtGui.QTextLayout.setText(string)
Parameters

string – str

Sets the layout’s text to the given string. The layout is invalidated and must be laid out again.

Notice that when using this QTextLayout as part of a QTextDocument this method will have no effect.

See also

text()

PySide6.QtGui.QTextLayout.setTextOption(option)
Parameters

optionPySide6.QtGui.QTextOption

Sets the text option structure that controls the layout process to the given option.

See also

textOption()

PySide6.QtGui.QTextLayout.text()
Return type

str

Returns the layout’s text.

See also

setText()

PySide6.QtGui.QTextLayout.textOption()
Return type

PySide6.QtGui.QTextOption

Returns the current text option used to control the layout process.

See also

setTextOption()