|  | Home · Overviews · Examples

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

The `QTextDocument` class holds formatted text that can be viewed and edited using a `QTextEdit`. More...

Inherits `QObject`.

## Detailed Description

The `QTextDocument` class holds formatted text that can be viewed and edited using a `QTextEdit`.

`QTextDocument` is a container for structured rich text documents, providing support for styled text and various types of document elements, such as lists, tables, frames, and images. They can be created for use in a `QTextEdit`, or used independently.

Each document element is described by an associated format object. Each format object is treated as a unique object by QTextDocuments, and can be passed to `objectForFormat` to obtain the document element that it is applied to.

A `QTextDocument` can be edited programmatically using a `QTextCursor`, and its contents can be examined by traversing the document structure. The entire document structure is stored as a hierarchy of document elements beneath the root frame, found with the `rootFrame` function. Alternatively, if you just want to iterate over the textual contents of the document you can use `begin`, `end`, and `findBlock` to retrieve text blocks that you can examine and iterate over.

The layout of a document is determined by the `documentLayout`; you can create your own `QAbstractTextDocumentLayout` subclass and set it using `setDocumentLayout` if you want to use your own layout logic. The document's title can be obtained by calling the documentTitle() function.

The `toPlainText` and `toHtml` convenience functions allow you to retrieve the contents of the document as plain text and HTML. The document's text can be searched using the `find` functions.

Undo/redo of operations performed on the document can be controlled using the `setUndoRedoEnabled` function. The undo/redo system can be controlled by an editor widget through the `undo` and `redo` slots; the document also provides `contentsChanged`, `undoAvailable`, and `redoAvailable` signals that inform connected editor widgets about the state of the undo/redo system.

See also `QTextCursor`, `QTextEdit`, and Rich Text Processing.