|  | Home · All Classes · Modules

# QTextLayout Class Reference
[QtGui module]

The QTextLayout class is used to lay out and paint a single paragraph of text. More...

### Types

- enum CursorMode { SkipCharacters, SkipWords }
- class FormatRange

### Methods

- __init__ (self)
- __init__ (self, QString text)
- __init__ (self, QString text, QFont font, QPaintDevice paintdevice = None)
- __init__ (self, QTextBlock b)
- QTextLayout.FormatRange-list additionalFormats (self)
- beginLayout (self)
- QRectF boundingRect (self)
- bool cacheEnabled (self)
- clearAdditionalFormats (self)
- clearLayout (self)
- QTextLine createLine (self)
- draw (self, QPainter p, QPointF pos, QTextLayout.FormatRange-list selections = QVector<QTextLayout::FormatRange>(), QRectF clip = QRectF())
- drawCursor (self, QPainter p, QPointF pos, int cursorPosition)
- drawCursor (self, QPainter p, QPointF pos, int cursorPosition, int width)
- endLayout (self)
- QFont font (self)
- bool isValidCursorPosition (self, int pos)
- QTextLine lineAt (self, int i)
- int lineCount (self)
- QTextLine lineForTextPosition (self, int pos)
- float maximumWidth (self)
- float minimumWidth (self)
- int nextCursorPosition (self, int oldPos, CursorMode mode = QTextLayout.SkipCharacters)
- QPointF position (self)
- int preeditAreaPosition (self)
- QString preeditAreaText (self)
- int previousCursorPosition (self, int oldPos, CursorMode mode = QTextLayout.SkipCharacters)
- setAdditionalFormats (self, QTextLayout.FormatRange-list overrides)
- setCacheEnabled (self, bool enable)
- setFont (self, QFont f)
- setPosition (self, QPointF p)
- setPreeditArea (self, int position, QString text)
- setText (self, QString string)
- setTextOption (self, QTextOption option)
- QString text (self)
- QTextOption textOption (self)

## Detailed Description

The QTextLayout class is used to lay out and paint a single paragraph of text.

It offers most 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 currently deal with plain text and rich text paragraphs that are part of a QTextDocument.

QTextLayout can be used to create a sequence of QTextLine's 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.

Here's some pseudo code that presents the layout phase:

     int leading = fontMetrics.leading();
     qreal height = 0;
     qreal widthUsed = 0;
     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();
         widthUsed = qMax(widthUsed, line.naturalTextWidth());
     }
     textLayout.endLayout();

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

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

The text layout's text is set in the constructor or with setText(). The layout can be seen as a sequence of QTextLine objects; use lineAt() or lineForTextPosition() to get a QTextLine, createLine() to create one. For a given position in the text you can find a valid cursor position with isValidCursorPosition(), nextCursorPosition(), and previousCursorPosition(). The layout itself can be positioned with setPosition(); it has a boundingRect(), and a minimumWidth() and a maximumWidth(). A text layout can be drawn on a painter device using draw().

## Type Documentation

### QTextLayout.CursorMode

Constant | Value
--- | ---
`QTextLayout.SkipCharacters` | `0`
`QTextLayout.SkipWords` | `1`

## Method Documentation

### QTextLayout.__init__ (self)

Constructs an empty text layout.

See also setText().

### QTextLayout.__init__ (self, QString text)

Constructs a text layout to lay out the given text.

### QTextLayout.__init__ (self, QString text, QFont font, QPaintDevice paintdevice = None)

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 0 the calculations will be done in screen metrics.

### QTextLayout.__init__ (self, QTextBlock b)

### QTextLayout.FormatRange-list QTextLayout.additionalFormats (self)

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

See also setAdditionalFormats() and clearAdditionalFormats().

### QTextLayout.beginLayout (self)

Begins the layout process.

### QRectF QTextLayout.boundingRect (self)

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

### bool QTextLayout.cacheEnabled (self)

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

See also setCacheEnabled().

### QTextLayout.clearAdditionalFormats (self)

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

See also additionalFormats() and setAdditionalFormats().

### QTextLayout.clearLayout (self)

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

This function was introduced in Qt 4.4.

### QTextLine QTextLayout.createLine (self)

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 QTextLine.setLineWidth() function is called.

Once QTextLine.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).

### QTextLayout.draw (self, QPainter p, QPointF pos, QTextLayout.FormatRange-list selections = QVector<QTextLayout::FormatRange>(), QRectF clip = QRectF())

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.

### QTextLayout.drawCursor (self, QPainter p, QPointF pos, int cursorPosition)

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.

### QTextLayout.drawCursor (self, QPainter p, QPointF pos, int cursorPosition, int width)

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.

### QTextLayout.endLayout (self)

Ends the layout process.

### QFont QTextLayout.font (self)

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

See also setFont().

### bool QTextLayout.isValidCursorPosition (self, int pos)

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 diaresis (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.

### QTextLine QTextLayout.lineAt (self, int i)

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

See also lineCount() and lineForTextPosition().

### int QTextLayout.lineCount (self)

Returns the number of lines in this text layout.

See also lineAt().

### QTextLine QTextLayout.lineForTextPosition (self, int pos)

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

See also isValidCursorPosition() and lineAt().

### float QTextLayout.maximumWidth (self)

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().

### float QTextLayout.minimumWidth (self)

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().

### int QTextLayout.nextCursorPosition (self, int oldPos, CursorMode mode = QTextLayout.SkipCharacters)

Returns the next valid cursor position after oldPos that respects the given cursor mode.

See also isValidCursorPosition() and previousCursorPosition().

### QPointF QTextLayout.position (self)

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

This function was introduced in Qt 4.2.

See also setPosition().

### int QTextLayout.preeditAreaPosition (self)

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

### QString QTextLayout.preeditAreaText (self)

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

### int QTextLayout.previousCursorPosition (self, int oldPos, CursorMode mode = QTextLayout.SkipCharacters)

Returns the first valid cursor position before oldPos that respects the given cursor mode.

See also isValidCursorPosition() and nextCursorPosition().

### QTextLayout.setAdditionalFormats (self, QTextLayout.FormatRange-list overrides)

Sets the additional formats supported by the text layout to formatList.

See also additionalFormats() and clearAdditionalFormats().

### QTextLayout.setCacheEnabled (self, bool enable)

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().

### QTextLayout.setFont (self, QFont f)

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

See also font() and text().

### QTextLayout.setPosition (self, QPointF p)

Moves the text layout to point p.

See also position().

### QTextLayout.setPreeditArea (self, int position, QString text)

Sets the position and text of the area in the layout that is processed before editing occurs.

### QTextLayout.setText (self, QString string)

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().

### QTextLayout.setTextOption (self, QTextOption option)

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

See also textOption() and QTextOption.

### QString QTextLayout.text (self)

Returns the layout's text.

See also setText().

### QTextOption QTextLayout.textOption (self)

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

See also setTextOption() and QTextOption.