QTextCursor Class

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

Header: #include <QTextCursor>
qmake: QT += gui

Note: All functions in this class are reentrant.

Public Types

enum MoveMode { MoveAnchor, KeepAnchor }
enum MoveOperation { NoMove, Start, StartOfLine, StartOfBlock, StartOfWord, …, PreviousRow }
enum SelectionType { Document, BlockUnderCursor, LineUnderCursor, WordUnderCursor }

Detailed Description

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. The cursor's current position() then is always either between two consecutive characters in the string, or else before the very first character or after the very last character in the string. 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 before 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 a part 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 between any two consecutive characters in the text, before the first character, or after the last character. 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.

Member Type Documentation

enum QTextCursor::MoveMode

ConstantValueDescription
QTextCursor::MoveAnchor0Moves the anchor to the same position as the cursor itself.
QTextCursor::KeepAnchor1Keeps the anchor where it is.

If the anchor() is kept where it is and the position() is moved, the text in between will be selected.

enum QTextCursor::MoveOperation

ConstantValueDescription
QTextCursor::NoMove0Keep the cursor where it is
QTextCursor::Start1Move to the start of the document.
QTextCursor::StartOfLine3Move to the start of the current line.
QTextCursor::StartOfBlock4Move to the start of the current block.
QTextCursor::StartOfWord5Move to the start of the current word.
QTextCursor::PreviousBlock6Move to the start of the previous block.
QTextCursor::PreviousCharacter7Move to the previous character.
QTextCursor::PreviousWord8Move to the beginning of the previous word.
QTextCursor::Up2Move up one line.
QTextCursor::Left9Move left one character.
QTextCursor::WordLeft10Move left one word.
QTextCursor::End11Move to the end of the document.
QTextCursor::EndOfLine13Move to the end of the current line.
QTextCursor::EndOfWord14Move to the end of the current word.
QTextCursor::EndOfBlock15Move to the end of the current block.
QTextCursor::NextBlock16Move to the beginning of the next block.
QTextCursor::NextCharacter17Move to the next character.
QTextCursor::NextWord18Move to the next word.
QTextCursor::Down12Move down one line.
QTextCursor::Right19Move right one character.
QTextCursor::WordRight20Move right one word.
QTextCursor::NextCell21Move to the beginning of the next table cell inside the current table. If the current cell is the last cell in the row, the cursor will move to the first cell in the next row.
QTextCursor::PreviousCell22Move to the beginning of the previous table cell inside the current table. If the current cell is the first cell in the row, the cursor will move to the last cell in the previous row.
QTextCursor::NextRow23Move to the first new cell of the next row in the current table.
QTextCursor::PreviousRow24Move to the last cell of the previous row in the current table.

See also movePosition().

enum QTextCursor::SelectionType

This enum describes the types of selection that can be applied with the select() function.

ConstantValueDescription
QTextCursor::Document3Selects the entire document.
QTextCursor::BlockUnderCursor2Selects the block of text under the cursor.
QTextCursor::LineUnderCursor1Selects the line of text under the cursor.
QTextCursor::WordUnderCursor0Selects the word under the cursor. If the cursor is not positioned within a string of selectable characters, no text is selected.