QTextCursor Class
The QTextCursor class is a position in the text of a QPlainTextEdit, with a selection, which can move and change the text. More…
| Header | #include <QTextCursor> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
List of all members, including inherited members
Public Types
| enum | MoveMode |
| enum | MoveOperation |
| enum | SelectionType |
Public Functions
| QTextCursor() | |
| bool | isNull() const |
| int | position() const |
| int | anchor() const |
| void | setPosition(int pos, MoveMode mode = MoveAnchor) |
| bool | movePosition(MoveOperation operation, MoveMode mode = MoveAnchor, int n = 1) |
| bool | atStart() const |
| bool | atEnd() const |
| bool | atBlockStart() const |
| bool | atBlockEnd() const |
| int | blockNumber() const |
| int | columnNumber() const |
| int | positionInBlock() const |
| bool | hasSelection() const |
| int | selectionStart() const |
| int | selectionEnd() const |
| QString | selectedText() const |
| void | clearSelection() |
| void | select(SelectionType selection) |
| void | insertText(const QString &text) |
| void | insertBlock() |
| void | removeSelectedText() |
| void | deleteChar() |
| void | deletePreviousChar() |
| void | beginEditBlock() |
| void | endEditBlock() |
| bool | operator==(const QTextCursor &other) const |
| bool | operator!=(const QTextCursor &other) const |
Detailed Description
A cursor has a position, and an anchor: the selection is the text between them. Get one from QPlainTextEdit::textCursor(), change it, and give it back with QPlainTextEdit::setTextCursor():
QTextCursor cursor = edit->textCursor();
cursor.movePosition(QTextCursor::EndOfBlock);
cursor.insertText(" (done)");
cursor.select(QTextCursor::BlockUnderCursor);
edit->setTextCursor(cursor);
Positions count the characters from the start of the text; a new line counts as one. Changing the text through a cursor changes the text of the editor at once, and the cursors that are after the change follow the text.
See also QPlainTextEdit.
Differences from Qt
The cursor belongs to a QPlainTextEdit, not to a QTextDocument, so it is in QtWidgets. There are no frames, tables, lists or formats (charFormat(), insertHtml(), insertFragment(), mergeCharFormat()), no joinPreviousEditBlock() and no visualNavigation(). selectedText() separates blocks with a new line and not with the paragraph separator.
Member Type Documentation
enum QTextCursor::MoveMode
This enum says what happens to the anchor when the cursor moves.
| Constant | Description |
|---|---|
QTextCursor::MoveAnchor | The anchor moves with the cursor: there is no selection. |
QTextCursor::KeepAnchor | The anchor stays, so the text that the cursor passed is selected. |
enum QTextCursor::MoveOperation
This enum says where movePosition() moves the cursor.
| Constant | Description |
|---|---|
QTextCursor::NoMove | It stays. |
QTextCursor::Start | The start of the text. |
QTextCursor::Up | One line up, in the same column if it can. |
QTextCursor::StartOfLine | The start of the line that is shown, which is a part of a block if the text wraps. |
QTextCursor::StartOfBlock | The start of the block: of the text up to a new line. |
QTextCursor::StartOfWord | The start of the word. |
QTextCursor::PreviousBlock | The start of the previous block. |
QTextCursor::PreviousCharacter | The character before. |
QTextCursor::PreviousWord | The start of the word before. |
QTextCursor::Left | One character to the left. |
QTextCursor::WordLeft | The start of the word before. |
QTextCursor::End | The end of the text. |
QTextCursor::Down | One line down, in the same column if it can. |
QTextCursor::EndOfLine | The end of the line that is shown. |
QTextCursor::EndOfWord | The end of the word. |
QTextCursor::EndOfBlock | The end of the block. |
QTextCursor::NextBlock | The start of the next block. |
QTextCursor::NextCharacter | The next character. |
QTextCursor::NextWord | The start of the next word. |
QTextCursor::Right | One character to the right. |
QTextCursor::WordRight | The start of the next word. |
enum QTextCursor::SelectionType
This enum says what select() selects.
| Constant | Description |
|---|---|
QTextCursor::WordUnderCursor | The word at the cursor. |
QTextCursor::LineUnderCursor | The line at the cursor, as it is shown. |
QTextCursor::BlockUnderCursor | The block at the cursor. |
QTextCursor::Document | All the text. |
Member Function Documentation
QTextCursor::QTextCursor()
Constructs a null cursor, which is in no editor.
int QTextCursor::anchor() const
Returns the position of the other end of the selection, which is the position if there is no selection.
See also position().
bool QTextCursor::atBlockEnd() const
Returns true if the cursor is at the end of a block.
bool QTextCursor::atBlockStart() const
Returns true if the cursor is at the start of a block.
bool QTextCursor::atEnd() const
Returns true if the cursor is at the end of the text.
bool QTextCursor::atStart() const
Returns true if the cursor is at the start of the text.
void QTextCursor::beginEditBlock()
Starts a group of changes that undo() undoes as one, until the matching endEditBlock(). The groups can be nested.
See also endEditBlock().
int QTextCursor::blockNumber() const
Returns the number of the block that the cursor is in, from 0.
See also columnNumber().
void QTextCursor::clearSelection()
Removes the selection, leaving the cursor where it is.
See also select().
int QTextCursor::columnNumber() const
Returns the number of characters between the start of the block and the cursor.
See also blockNumber().
void QTextCursor::deleteChar()
Removes the selected text, or the character after the cursor if nothing is selected.
See also deletePreviousChar().
void QTextCursor::deletePreviousChar()
Removes the selected text, or the character before the cursor if nothing is selected.
See also deleteChar().
void QTextCursor::endEditBlock()
Ends the group of changes that beginEditBlock() started.
See also beginEditBlock().
bool QTextCursor::hasSelection() const
Returns true if the cursor has selected text.
See also selectedText().
void QTextCursor::insertBlock()
Starts a new block at the cursor, by inserting a new line.
See also insertText().
void QTextCursor::insertText(const QString &text)
Replaces the selection with text, or inserts it at the cursor if there is none. The cursor is after the text.
See also removeSelectedText().
bool QTextCursor::isNull() const
Returns true if the cursor is in no editor, or the editor was deleted.
bool QTextCursor::movePosition(MoveOperation operation, MoveMode mode = MoveAnchor, int n = 1)
Moves the cursor by operation, n times for the operations that can repeat. Returns true if the cursor moved. With KeepAnchor for mode, the text that it passes is selected.
See also setPosition().
bool QTextCursor::operator!=(const QTextCursor &other) const
Returns true if this cursor and other are not equal.
bool QTextCursor::operator==(const QTextCursor &other) const
Returns true if this cursor and other are in the same editor, at the same position and with the same anchor.
int QTextCursor::position() const
Returns the position of the cursor, which is the end of the selection that moves.
See also anchor() and setPosition().
int QTextCursor::positionInBlock() const
Returns the same as columnNumber().
void QTextCursor::removeSelectedText()
Removes the selected text, if there is some.
See also deleteChar().
void QTextCursor::select(SelectionType selection)
Selects the text that selection says: the cursor is at the end of it, and the anchor at the start.
QString QTextCursor::selectedText() const
Returns the selected text. A selection of several blocks has a new line between them.
See also hasSelection().
int QTextCursor::selectionEnd() const
Returns the end of the selection: the larger of the position and the anchor.
int QTextCursor::selectionStart() const
Returns the start of the selection: the smaller of the position and the anchor.
void QTextCursor::setPosition(int pos, MoveMode mode = MoveAnchor)
Moves the cursor to pos, which is brought into the text. The anchor moves too if mode is MoveAnchor, and otherwise the text between it and the new position is selected.
See also position() and movePosition().