6.0.0-rc4Source

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>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes

List of all members, including inherited members

Public Types

Public Functions

QTextCursor()
boolisNull() const
intposition() const
intanchor() const
voidsetPosition(int pos, MoveMode mode = MoveAnchor)
boolmovePosition(MoveOperation operation, MoveMode mode = MoveAnchor, int n = 1)
boolatStart() const
boolatEnd() const
boolatBlockStart() const
boolatBlockEnd() const
intblockNumber() const
intcolumnNumber() const
intpositionInBlock() const
boolhasSelection() const
intselectionStart() const
intselectionEnd() const
QStringselectedText() const
voidclearSelection()
voidselect(SelectionType selection)
voidinsertText(const QString &text)
voidinsertBlock()
voidremoveSelectedText()
voiddeleteChar()
voiddeletePreviousChar()
voidbeginEditBlock()
voidendEditBlock()
booloperator==(const QTextCursor &other) const
booloperator!=(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.

ConstantDescription
QTextCursor::MoveAnchorThe anchor moves with the cursor: there is no selection.
QTextCursor::KeepAnchorThe anchor stays, so the text that the cursor passed is selected.

enum QTextCursor::MoveOperation

This enum says where movePosition() moves the cursor.

ConstantDescription
QTextCursor::NoMoveIt stays.
QTextCursor::StartThe start of the text.
QTextCursor::UpOne line up, in the same column if it can.
QTextCursor::StartOfLineThe start of the line that is shown, which is a part of a block if the text wraps.
QTextCursor::StartOfBlockThe start of the block: of the text up to a new line.
QTextCursor::StartOfWordThe start of the word.
QTextCursor::PreviousBlockThe start of the previous block.
QTextCursor::PreviousCharacterThe character before.
QTextCursor::PreviousWordThe start of the word before.
QTextCursor::LeftOne character to the left.
QTextCursor::WordLeftThe start of the word before.
QTextCursor::EndThe end of the text.
QTextCursor::DownOne line down, in the same column if it can.
QTextCursor::EndOfLineThe end of the line that is shown.
QTextCursor::EndOfWordThe end of the word.
QTextCursor::EndOfBlockThe end of the block.
QTextCursor::NextBlockThe start of the next block.
QTextCursor::NextCharacterThe next character.
QTextCursor::NextWordThe start of the next word.
QTextCursor::RightOne character to the right.
QTextCursor::WordRightThe start of the next word.

enum QTextCursor::SelectionType

This enum says what select() selects.

ConstantDescription
QTextCursor::WordUnderCursorThe word at the cursor.
QTextCursor::LineUnderCursorThe line at the cursor, as it is shown.
QTextCursor::BlockUnderCursorThe block at the cursor.
QTextCursor::DocumentAll 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().