|  | Home · Overviews · Examples

# QTextLayout Class Reference
[com.trolltech.qt.gui module]

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

## 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();
        int height = 0;
        qreal widthUsed = 0;
        textLayout.beginLayout();
        while (1) {
            QTextLine line = textLayout.createLine();
            if (!line.isValid())
                break;

            line.setLineWidth(lineWidth);
            height += leading;
            line.setPosition(QPoint(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`.