|  | Home · Overviews · Examples |

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

The `QTextCursor` class offers an API to access and modify QTextDocuments. More...

## Detailed Description

The `QTextCursor` class offers an API to access and modify QTextDocuments.

Text cursors are objects that are used to access and modify the contents and underlying structure of text documents via a programming interface that mimics the behavior of a cursor in a text editor. `QTextCursor` contains information about both the cursor's position within a `QTextDocument` and any selection that it has made.

`QTextCursor` is modeled on the way a text cursor behaves in a text editor, providing a programmatic means of performing standard actions through the user interface. A document can be thought of as a single string of characters with the cursor's `position` being between any two characters (or at the very beginning or very end of the document). Documents can also contain tables, lists, images, and other objects in addition to text but, from the developer's point of view, the document can be treated as one long string. Some portions of that string can be considered to lie within particular blocks (e.g. paragraphs), or within a table's cell, or a list's item, or other structural elements. When we refer to "current character" we mean the character immediately after the cursor `position` in the document; similarly the "current block" is the block that contains the cursor `position`.

A `QTextCursor` also has an `anchor` position. The text that is between the `anchor` and the `position` is the selection. If `anchor` == `position` there is no selection.

The cursor position can be changed programmatically using `setPosition` and `movePosition`; the latter can also be used to select text. For selections see `selectionStart`, `selectionEnd`, `hasSelection`, `clearSelection`, and `removeSelectedText`.

If the `position` is at the start of a block `atBlockStart` returns true; and if it is at the end of a block `atBlockEnd` returns true. The format of the current character is returned by `charFormat`, and the format of the current block is returned by `blockFormat`.

Formatting can be applied to the current text document using the `setCharFormat`, `mergeCharFormat`, `setBlockFormat` and `mergeBlockFormat` functions. The 'set' functions will replace the cursor's current character or block format, while the 'merge' functions add the given format properties to the cursor's current format. If the cursor has a selection the given format is applied to the current selection. Note that when only parts of a block is selected the block format is applied to the entire block. The text at the current character position can be turned into a list using `createList`.

Deletions can be achieved using `deleteChar`, `deletePreviousChar`, and `removeSelectedText`.

Text strings can be inserted into the document with the `insertText` function, blocks (representing new paragraphs) can be inserted with `insertBlock`.

Existing fragments of text can be inserted with `insertFragment` but, if you want to insert pieces of text in various formats, it is usually still easier to use `insertText` and supply a character format.

Various types of higher-level structure can also be inserted into the document with the cursor:

- Lists are ordered sequences of block elements that are decorated with bullet points or symbols. These are inserted in a specified format with `insertList`.
- Tables are inserted with the `insertTable` function, and can be given an optional format. These contain an array of cells that can be traversed using the cursor.
- Inline images are inserted with `insertImage`. The image to be used can be specified in an image format, or by name.
- Frames are inserted by calling `insertFrame` with a specified format.

Actions can be grouped (i.e. treated as a single action for undo/redo) using `beginEditBlock` and `endEditBlock`.

Cursor movements are limited to valid cursor positions. In Latin writing this is usually after every character in the text. In some other writing systems cursor movements are limited to "clusters" (e.g. a syllable in Devanagari, or a base letter plus diacritics). Functions such as `movePosition` and `deleteChar` limit cursor movement to these valid positions.

See also Rich Text Processing.