Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QTextCursor Class Reference

\reentrant \inmodule QtGui More...

#include <qtextcursor.h>

+ Collaboration diagram for QTextCursor:

Public Types

enum  MoveMode { MoveAnchor , KeepAnchor }
 \value MoveAnchor Moves the anchor to the same position as the cursor itself. More...
 
enum  MoveOperation {
  NoMove , Start , Up , StartOfLine ,
  StartOfBlock , StartOfWord , PreviousBlock , PreviousCharacter ,
  PreviousWord , Left , WordLeft , End ,
  Down , EndOfLine , EndOfWord , EndOfBlock ,
  NextBlock , NextCharacter , NextWord , Right ,
  WordRight , NextCell , PreviousCell , NextRow ,
  PreviousRow
}
 \value NoMove Keep the cursor where it is More...
 
enum  SelectionType { WordUnderCursor , LineUnderCursor , BlockUnderCursor , Document }
 This enum describes the types of selection that can be applied with the select() function. More...
 

Public Member Functions

 QTextCursor ()
 Constructs a null cursor.
 
 QTextCursor (QTextDocument *document)
 Constructs a cursor pointing to the beginning of the document.
 
 QTextCursor (QTextDocumentPrivate *p, int pos)
 
 QTextCursor (QTextCursorPrivate *d)
 
 QTextCursor (QTextFrame *frame)
 Constructs a cursor pointing to the beginning of the frame.
 
 QTextCursor (const QTextBlock &block)
 Constructs a cursor pointing to the beginning of the block.
 
 QTextCursor (const QTextCursor &cursor)
 Constructs a new cursor that is a copy of cursor.
 
QTextCursoroperator= (const QTextCursor &other)
 Makes a copy of cursor and assigns it to this QTextCursor.
 
 ~QTextCursor ()
 Destroys the QTextCursor.
 
void swap (QTextCursor &other) noexcept
 
bool isNull () const
 Returns true if the cursor is null; otherwise returns false.
 
void setPosition (int pos, MoveMode mode=MoveAnchor)
 Moves the cursor to the absolute position in the document specified by pos using a MoveMode specified by m.
 
int position () const
 Returns the absolute position of the cursor within the document.
 
int positionInBlock () const
 
int anchor () const
 Returns the anchor position; this is the same as position() unless there is a selection in which case position() marks one end of the selection and anchor() marks the other end.
 
void insertText (const QString &text)
 Inserts text at the current position, using the current character format.
 
void insertText (const QString &text, const QTextCharFormat &format)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Inserts text at the current position with the given format.
 
bool movePosition (MoveOperation op, MoveMode=MoveAnchor, int n=1)
 Moves the cursor by performing the given operation n times, using the specified mode, and returns true if all operations were completed successfully; otherwise returns false.
 
bool visualNavigation () const
 
void setVisualNavigation (bool b)
 
void setVerticalMovementX (int x)
 
int verticalMovementX () const
 
void setKeepPositionOnInsert (bool b)
 
bool keepPositionOnInsert () const
 
void deleteChar ()
 If there is no selected text, deletes the character at the current cursor position; otherwise deletes the selected text.
 
void deletePreviousChar ()
 If there is no selected text, deletes the character before the current cursor position; otherwise deletes the selected text.
 
void select (SelectionType selection)
 Selects text in the document according to the given selection.
 
bool hasSelection () const
 Returns true if the cursor contains a selection; otherwise returns false.
 
bool hasComplexSelection () const
 Returns true if the cursor contains a selection that is not simply a range from selectionStart() to selectionEnd(); otherwise returns false.
 
void removeSelectedText ()
 If there is a selection, its content is deleted; otherwise does nothing.
 
void clearSelection ()
 Clears the current selection by setting the anchor to the cursor position.
 
int selectionStart () const
 Returns the start of the selection or position() if the cursor doesn't have a selection.
 
int selectionEnd () const
 Returns the end of the selection or position() if the cursor doesn't have a selection.
 
QString selectedText () const
 Returns the current selection's text (which may be empty).
 
QTextDocumentFragment selection () const
 Returns the current selection (which may be empty) with all its formatting information.
 
void selectedTableCells (int *firstRow, int *numRows, int *firstColumn, int *numColumns) const
 If the selection spans over table cells, firstRow is populated with the number of the first row in the selection, firstColumn with the number of the first column in the selection, and numRows and numColumns with the number of rows and columns in the selection.
 
QTextBlock block () const
 Returns the block that contains the cursor.
 
QTextCharFormat charFormat () const
 Returns the format of the character immediately before the cursor position().
 
void setCharFormat (const QTextCharFormat &format)
 Sets the cursor's current character format to the given format.
 
void mergeCharFormat (const QTextCharFormat &modifier)
 Merges the cursor's current character format with the properties described by format modifier.
 
QTextBlockFormat blockFormat () const
 Returns the block format of the block the cursor is in.
 
void setBlockFormat (const QTextBlockFormat &format)
 Sets the block format of the current block (or all blocks that are contained in the selection) to format.
 
void mergeBlockFormat (const QTextBlockFormat &modifier)
 Modifies the block format of the current block (or all blocks that are contained in the selection) with the block format specified by modifier.
 
QTextCharFormat blockCharFormat () const
 Returns the block character format of the block the cursor is in.
 
void setBlockCharFormat (const QTextCharFormat &format)
 Sets the block char format of the current block (or all blocks that are contained in the selection) to format.
 
void mergeBlockCharFormat (const QTextCharFormat &modifier)
 Modifies the block char format of the current block (or all blocks that are contained in the selection) with the block format specified by modifier.
 
bool atBlockStart () const
 Returns true if the cursor is at the start of a block; otherwise returns false.
 
bool atBlockEnd () const
 Returns true if the cursor is at the end of a block; otherwise returns false.
 
bool atStart () const
 Returns true if the cursor is at the start of the document; otherwise returns false.
 
bool atEnd () const
 
void insertBlock ()
 Inserts a new empty block at the cursor position() with the current blockFormat() and charFormat().
 
void insertBlock (const QTextBlockFormat &format)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Inserts a new empty block at the cursor position() with block format format and the current charFormat() as block char format.
 
void insertBlock (const QTextBlockFormat &format, const QTextCharFormat &charFormat)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Inserts a new empty block at the cursor position() with block format format and charFormat as block char format.
 
QTextListinsertList (const QTextListFormat &format)
 Inserts a new block at the current position and makes it the first list item of a newly created list with the given format.
 
QTextListinsertList (QTextListFormat::Style style)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Inserts a new block at the current position and makes it the first list item of a newly created list with the given style.
 
QTextListcreateList (const QTextListFormat &format)
 Creates and returns a new list with the given format, and makes the current paragraph the cursor is in the first list item.
 
QTextListcreateList (QTextListFormat::Style style)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Creates and returns a new list with the given style, making the cursor's current paragraph the first list item.
 
QTextListcurrentList () const
 Returns the current list if the cursor position() is inside a block that is part of a list; otherwise returns \nullptr.
 
QTextTableinsertTable (int rows, int cols, const QTextTableFormat &format)
 Creates a new table with the given number of rows and columns in the specified format, inserts it at the current cursor position() in the document, and returns the table object.
 
QTextTableinsertTable (int rows, int cols)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Creates a new table with the given number of rows and columns, inserts it at the current cursor position() in the document, and returns the table object.
 
QTextTablecurrentTable () const
 Returns a pointer to the current table if the cursor position() is inside a block that is part of a table; otherwise returns \nullptr.
 
QTextFrameinsertFrame (const QTextFrameFormat &format)
 Inserts a frame with the given format at the current cursor position(), moves the cursor position() inside the frame, and returns the frame.
 
QTextFramecurrentFrame () const
 Returns a pointer to the current frame.
 
void insertFragment (const QTextDocumentFragment &fragment)
 Inserts the text fragment at the current position().
 
void insertHtml (const QString &html)
 
void insertImage (const QTextImageFormat &format, QTextFrameFormat::Position alignment)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
void insertImage (const QTextImageFormat &format)
 Inserts the image defined by format at the current position().
 
void insertImage (const QString &name)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Convenience method for inserting the image with the given name at the current position().
 
void insertImage (const QImage &image, const QString &name=QString())
 
void beginEditBlock ()
 Indicates the start of a block of editing operations on the document that should appear as a single operation from an undo/redo point of view.
 
void joinPreviousEditBlock ()
 Like beginEditBlock() indicates the start of a block of editing operations that should appear as a single operation for undo/redo.
 
void endEditBlock ()
 Indicates the end of a block of editing operations on the document that should appear as a single operation from an undo/redo point of view.
 
bool operator!= (const QTextCursor &rhs) const
 Returns true if the other cursor is at a different position in the document as this cursor; otherwise returns false.
 
bool operator< (const QTextCursor &rhs) const
 Returns true if the other cursor is positioned later in the document than this cursor; otherwise returns false.
 
bool operator<= (const QTextCursor &rhs) const
 Returns true if the other cursor is positioned later or at the same position in the document as this cursor; otherwise returns false.
 
bool operator== (const QTextCursor &rhs) const
 Returns true if the other cursor is at the same position in the document as this cursor; otherwise returns false.
 
bool operator>= (const QTextCursor &rhs) const
 Returns true if the other cursor is positioned earlier or at the same position in the document as this cursor; otherwise returns false.
 
bool operator> (const QTextCursor &rhs) const
 Returns true if the other cursor is positioned earlier in the document than this cursor; otherwise returns false.
 
bool isCopyOf (const QTextCursor &other) const
 Returns true if this cursor and other are copies of each other, i.e.
 
int blockNumber () const
 
int columnNumber () const
 
QTextDocumentdocument () const
 

Friends

class QTextCursorPrivate
 
class QTextDocumentPrivate
 
class QTextDocumentFragmentPrivate
 
class QTextCopyHelper
 
class QWidgetTextControlPrivate
 

Detailed Description

\reentrant \inmodule QtGui

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. 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:

\list

  • 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. \endlist

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}

Definition at line 29 of file qtextcursor.h.

Member Enumeration Documentation

◆ MoveMode

\value MoveAnchor Moves the anchor to the same position as the cursor itself.

\value KeepAnchor Keeps 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.

Enumerator
MoveAnchor 
KeepAnchor 

Definition at line 47 of file qtextcursor.h.

◆ MoveOperation

\value NoMove Keep the cursor where it is

\value Start Move to the start of the document. \value StartOfLine Move to the start of the current line. \value StartOfBlock Move to the start of the current block. \value StartOfWord Move to the start of the current word. \value PreviousBlock Move to the start of the previous block. \value PreviousCharacter Move to the previous character. \value PreviousWord Move to the beginning of the previous word. \value Up Move up one line. \value Left Move left one character. \value WordLeft Move left one word.

\value End Move to the end of the document. \value EndOfLine Move to the end of the current line. \value EndOfWord Move to the end of the current word. \value EndOfBlock Move to the end of the current block. \value NextBlock Move to the beginning of the next block. \value NextCharacter Move to the next character. \value NextWord Move to the next word. \value Down Move down one line. \value Right Move right one character. \value WordRight Move right one word.

\value NextCell Move 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. \value PreviousCell Move 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. \value NextRow Move to the first new cell of the next row in the current table. \value PreviousRow Move to the last cell of the previous row in the current table.

See also
movePosition()
Enumerator
NoMove 
Start 
Up 
StartOfLine 
StartOfBlock 
StartOfWord 
PreviousBlock 
PreviousCharacter 
PreviousWord 
Left 
WordLeft 
End 
Down 
EndOfLine 
EndOfWord 
EndOfBlock 
NextBlock 
NextCharacter 
NextWord 
Right 
WordRight 
NextCell 
PreviousCell 
NextRow 
PreviousRow 

Definition at line 61 of file qtextcursor.h.

◆ SelectionType

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

\value Document Selects the entire document. \value BlockUnderCursor Selects the block of text under the cursor. \value LineUnderCursor Selects the line of text under the cursor. \value WordUnderCursor Selects the word under the cursor. If the cursor is not positioned within a string of selectable characters, no text is selected.

Enumerator
WordUnderCursor 
LineUnderCursor 
BlockUnderCursor 
Document 

Definition at line 106 of file qtextcursor.h.

Constructor & Destructor Documentation

◆ QTextCursor() [1/7]

QTextCursor::QTextCursor ( )

Constructs a null cursor.

Definition at line 1010 of file qtextcursor.cpp.

◆ QTextCursor() [2/7]

QTextCursor::QTextCursor ( QTextDocument * document)
explicit

Constructs a cursor pointing to the beginning of the document.

Definition at line 1018 of file qtextcursor.cpp.

◆ QTextCursor() [3/7]

QTextCursor::QTextCursor ( QTextDocumentPrivate * p,
int pos )

Definition at line 1046 of file qtextcursor.cpp.

References QTextCursorPrivate::adjusted_anchor, QTextCursorPrivate::anchor, pos, QTextCursorPrivate::position, and QTextCursorPrivate::setX().

+ Here is the call graph for this function:

◆ QTextCursor() [4/7]

QTextCursor::QTextCursor ( QTextCursorPrivate * d)
explicit

Definition at line 1057 of file qtextcursor.cpp.

References d, and Q_ASSERT.

◆ QTextCursor() [5/7]

QTextCursor::QTextCursor ( QTextFrame * frame)
explicit

Constructs a cursor pointing to the beginning of the frame.

Definition at line 1026 of file qtextcursor.cpp.

References QTextCursorPrivate::adjusted_anchor, QTextCursorPrivate::anchor, frame, and QTextCursorPrivate::position.

◆ QTextCursor() [6/7]

QTextCursor::QTextCursor ( const QTextBlock & block)
explicit

Constructs a cursor pointing to the beginning of the block.

Definition at line 1036 of file qtextcursor.cpp.

References QTextCursorPrivate::adjusted_anchor, QTextCursorPrivate::anchor, block(), QTextCursorPrivate::position, and QTextBlock::position().

+ Here is the call graph for this function:

◆ QTextCursor() [7/7]

QTextCursor::QTextCursor ( const QTextCursor & cursor)

Constructs a new cursor that is a copy of cursor.

Definition at line 1066 of file qtextcursor.cpp.

References cursor.

◆ ~QTextCursor()

QTextCursor::~QTextCursor ( )

Destroys the QTextCursor.

Definition at line 1094 of file qtextcursor.cpp.

Member Function Documentation

◆ anchor()

int QTextCursor::anchor ( ) const

Returns the anchor position; this is the same as position() unless there is a selection in which case position() marks one end of the selection and anchor() marks the other end.

Just like the cursor position, the anchor position is between characters.

See also
position(), setPosition(), movePosition(), selectionStart(), selectionEnd()

Definition at line 1199 of file qtextcursor.cpp.

References QTextCursorPrivate::anchor, and QTextCursorPrivate::priv.

Referenced by QWidgetTextControlPrivate::cursorMoveKeyEvent(), QQuickTextControlPrivate::cursorMoveKeyEvent(), QWidgetTextControlPrivate::repaintOldAndNewSelection(), QQuickTextControlPrivate::repaintOldAndNewSelection(), QWidgetTextControlPrivate::selectionChanged(), and setPosition().

+ Here is the caller graph for this function:

◆ atBlockEnd()

bool QTextCursor::atBlockEnd ( ) const

Returns true if the cursor is at the end of a block; otherwise returns false.

See also
atBlockStart(), atEnd()

Definition at line 1953 of file qtextcursor.cpp.

References QTextCursorPrivate::block(), QTextBlock::length(), QTextCursorPrivate::position, QTextBlock::position(), and QTextCursorPrivate::priv.

Referenced by QWidgetTextControlPrivate::keyPressEvent(), and QQuickTextControlPrivate::keyPressEvent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ atBlockStart()

bool QTextCursor::atBlockStart ( ) const

Returns true if the cursor is at the start of a block; otherwise returns false.

See also
atBlockEnd(), atStart()

Definition at line 1939 of file qtextcursor.cpp.

References QTextCursorPrivate::block(), QTextCursorPrivate::position, QTextBlock::position(), and QTextCursorPrivate::priv.

Referenced by QWidgetTextControlPrivate::keyPressEvent(), and QQuickTextControlPrivate::keyPressEvent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ atEnd()

bool QTextCursor::atEnd ( ) const
Since
4.6

Returns true if the cursor is at the end of the document; otherwise returns false.

See also
atStart(), atBlockEnd()

Definition at line 1983 of file qtextcursor.cpp.

References QTextDocumentPrivate::length(), QTextCursorPrivate::position, and QTextCursorPrivate::priv.

+ Here is the call graph for this function:

◆ atStart()

bool QTextCursor::atStart ( ) const

Returns true if the cursor is at the start of the document; otherwise returns false.

See also
atBlockStart(), atEnd()

Definition at line 1967 of file qtextcursor.cpp.

References QTextCursorPrivate::position, and QTextCursorPrivate::priv.

◆ beginEditBlock()

void QTextCursor::beginEditBlock ( )

Indicates the start of a block of editing operations on the document that should appear as a single operation from an undo/redo point of view.

For example:

cursor.beginEditBlock();
cursor.insertText("Hello");
cursor.insertText("World");
cursor.endEditBlock();

The call to undo() will cause both insertions to be undone, causing both "World" and "Hello" to be removed.

It is possible to nest calls to beginEditBlock and endEditBlock. The top-most pair will determine the scope of the undo/redo operation.

See also
endEditBlock()

Definition at line 2475 of file qtextcursor.cpp.

References QTextDocumentPrivate::beginEditBlock(), QTextCursorPrivate::position, and QTextCursorPrivate::priv.

Referenced by QWidgetTextControlPrivate::append(), QWidgetTextControlPrivate::commitPreedit(), QWidgetTextControlPrivate::createAutoBulletList(), QWidgetTextControlPrivate::dropEvent(), QTextHtmlImporter::import(), QTextMarkdownImporter::import(), and QWidgetTextControlPrivate::inputMethodEvent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ block()

◆ blockCharFormat()

QTextCharFormat QTextCursor::blockCharFormat ( ) const

Returns the block character format of the block the cursor is in.

The block char format is the format used when inserting text at the beginning of an empty block.

See also
setBlockCharFormat()

Definition at line 1814 of file qtextcursor.cpp.

References QTextCursorPrivate::block(), QTextBlock::charFormat(), and QTextCursorPrivate::priv.

+ Here is the call graph for this function:

◆ blockFormat()

QTextBlockFormat QTextCursor::blockFormat ( ) const

Returns the block format of the block the cursor is in.

See also
setBlockFormat(), charFormat()

Definition at line 1769 of file qtextcursor.cpp.

References QTextCursorPrivate::block(), QTextBlock::blockFormat(), and QTextCursorPrivate::priv.

Referenced by QQuickTextSelection::alignment(), QWidgetTextControlPrivate::append(), QTextMarkdownImporter::cbEnterBlock(), QTextMarkdownImporter::cbText(), QWidgetTextControlPrivate::createAutoBulletList(), currentList(), QTextHtmlImporter::import(), QWidgetTextControlPrivate::indent(), insertBlock(), QWidgetTextControlPrivate::insertParagraphSeparator(), insertText(), QWidgetTextControlPrivate::keyPressEvent(), QQuickTextControlPrivate::keyPressEvent(), and QWidgetTextControlPrivate::outdent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ blockNumber()

int QTextCursor::blockNumber ( ) const
Since
4.2 Returns the number of the block the cursor is in, or 0 if the cursor is invalid.

Note that this function only makes sense in documents without complex objects such as tables or frames.

Definition at line 2543 of file qtextcursor.cpp.

References QTextCursorPrivate::block(), QTextBlock::blockNumber(), and QTextCursorPrivate::priv.

+ Here is the call graph for this function:

◆ charFormat()

QTextCharFormat QTextCursor::charFormat ( ) const

Returns the format of the character immediately before the cursor position().

If the cursor is positioned at the beginning of a text block that is not empty then the format of the character immediately after the cursor is returned.

See also
insertText(), blockFormat()

Definition at line 1859 of file qtextcursor.cpp.

References block(), QTextCursorPrivate::block(), QTextDocumentPrivate::blockCharFormatIndex(), QTextDocumentPrivate::blockMap(), QTextFormatCollection::charFormat(), QTextFormat::clearProperty(), QTextCursorPrivate::currentCharFormat, QTextDocumentPrivate::find(), QFragmentMap< Fragment >::firstNode(), QTextDocumentPrivate::formatCollection(), it, QTextBlock::length(), QTextFormat::ObjectIndex, pos, QTextCursorPrivate::position, QTextBlock::position(), priv(), QTextCursorPrivate::priv, and Q_ASSERT.

Referenced by QWidgetTextControlPrivate::activateLinkUnderCursor(), QQuickTextControlPrivate::activateLinkUnderCursor(), QWidgetTextControlPrivate::anchorForCursor(), QWidgetTextControlPrivate::append(), QQuickTextSelection::color(), QQuickTextSelection::font(), QWidgetTextControlPrivate::inputMethodEvent(), insertBlock(), insertBlock(), QWidgetTextControlPrivate::insertParagraphSeparator(), insertText(), mergeCharFormat(), QQuickTextControlPrivate::setContent(), QWidgetTextControlPrivate::setContent(), QWidgetTextControlPrivate::updateCurrentCharFormat(), and QQuickTextControlPrivate::updateCurrentCharFormat().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ clearSelection()

void QTextCursor::clearSelection ( )

Clears the current selection by setting the anchor to the cursor position.

Note that it does {not} delete the text of the selection.

See also
removeSelectedText(), hasSelection()

Definition at line 1622 of file qtextcursor.cpp.

References QTextCursorPrivate::adjusted_anchor, QTextCursorPrivate::anchor, QTextCursorPrivate::currentCharFormat, and QTextCursorPrivate::position.

Referenced by QWidgetTextControlPrivate::activateLinkUnderCursor(), QQuickTextControlPrivate::activateLinkUnderCursor(), QWidgetTextControlPrivate::focusEvent(), QQuickTextControlPrivate::focusEvent(), QWidgetTextControlPrivate::mousePressEvent(), QQuickTextControlPrivate::mousePressEvent(), QWidgetTextControlPrivate::mouseReleaseEvent(), select(), and QWidgetTextControl::setFocusToNextOrPreviousAnchor().

+ Here is the caller graph for this function:

◆ columnNumber()

int QTextCursor::columnNumber ( ) const
Since
4.2 Returns the position of the cursor within its containing line.

Note that this is the column number relative to a wrapped line, not relative to the block (i.e. the paragraph).

You probably want to call positionInBlock() instead.

See also
positionInBlock()

Definition at line 2563 of file qtextcursor.cpp.

References block(), QTextCursorPrivate::block(), QTextCursorPrivate::blockLayout(), QTextBlock::isValid(), layout, line, QTextCursorPrivate::position, QTextBlock::position(), and QTextCursorPrivate::priv.

+ Here is the call graph for this function:

◆ createList() [1/2]

QTextList * QTextCursor::createList ( const QTextListFormat & format)

Creates and returns a new list with the given format, and makes the current paragraph the cursor is in the first list item.

See also
insertList(), currentList()

Definition at line 2075 of file qtextcursor.cpp.

References QTextDocumentPrivate::createObject(), list, mergeBlockFormat(), QTextCursorPrivate::priv, and QTextFormat::setObjectIndex().

Referenced by QTextMarkdownImporter::cbLeaveBlock(), QWidgetTextControlPrivate::createAutoBulletList(), createList(), QWidgetTextControlPrivate::indent(), insertList(), and insertList().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createList() [2/2]

QTextList * QTextCursor::createList ( QTextListFormat::Style style)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Creates and returns a new list with the given style, making the cursor's current paragraph the first list item.

The style to be used is defined by the QTextListFormat::Style enum.

See also
insertList(), currentList()

Definition at line 2097 of file qtextcursor.cpp.

References createList(), fmt, and QTextListFormat::setStyle().

+ Here is the call graph for this function:

◆ currentFrame()

QTextFrame * QTextCursor::currentFrame ( ) const

Returns a pointer to the current frame.

Returns \nullptr if the cursor is invalid.

See also
insertFrame()

Definition at line 2207 of file qtextcursor.cpp.

References QTextDocumentPrivate::frameAt(), QTextCursorPrivate::position, and QTextCursorPrivate::priv.

Referenced by QWidgetTextControlPrivate::repaintOldAndNewSelection(), and QQuickTextControlPrivate::repaintOldAndNewSelection().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ currentList()

QTextList * QTextCursor::currentList ( ) const

Returns the current list if the cursor position() is inside a block that is part of a list; otherwise returns \nullptr.

See also
insertList(), createList()

Definition at line 2110 of file qtextcursor.cpp.

References blockFormat(), o, QTextDocumentPrivate::objectForFormat(), and QTextCursorPrivate::priv.

Referenced by QTextMarkdownImporter::cbText(), QWidgetTextControlPrivate::indent(), QWidgetTextControlPrivate::insertParagraphSeparator(), QWidgetTextControlPrivate::keyPressEvent(), QQuickTextControlPrivate::keyPressEvent(), and QWidgetTextControlPrivate::outdent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ currentTable()

QTextTable * QTextCursor::currentTable ( ) const

Returns a pointer to the current table if the cursor position() is inside a block that is part of a table; otherwise returns \nullptr.

See also
insertTable()

Definition at line 2170 of file qtextcursor.cpp.

References frame, QTextDocumentPrivate::frameAt(), QTextCursorPrivate::position, and QTextCursorPrivate::priv.

Referenced by QTextCopyHelper::copy(), QWidgetTextControlPrivate::gotoNextTableCell(), and QWidgetTextControlPrivate::gotoPreviousTableCell().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ deleteChar()

void QTextCursor::deleteChar ( )

If there is no selected text, deletes the character at the current cursor position; otherwise deletes the selected text.

See also
deletePreviousChar(), hasSelection(), clearSelection()

Definition at line 1475 of file qtextcursor.cpp.

References QTextCursorPrivate::adjusted_anchor, QTextCursorPrivate::anchor, QTextCursorPrivate::canDelete(), QTextDocumentPrivate::nextCursorPosition(), QTextCursorPrivate::position, QTextCursorPrivate::priv, QTextCursorPrivate::remove(), removeSelectedText(), QTextCursorPrivate::setX(), and QTextLayout::SkipCharacters.

Referenced by QWidgetTextControlPrivate::keyPressEvent(), and QQuickTextControlPrivate::keyPressEvent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ deletePreviousChar()

void QTextCursor::deletePreviousChar ( )

If there is no selected text, deletes the character before the current cursor position; otherwise deletes the selected text.

See also
deleteChar(), hasSelection(), clearSelection()

Definition at line 1499 of file qtextcursor.cpp.

References QTextCursorPrivate::adjusted_anchor, QTextCursorPrivate::anchor, QString::at(), QTextDocumentPrivate::buffer(), QTextCursorPrivate::canDelete(), QTextDocumentPrivate::find(), QTextCursorPrivate::position, QTextCursorPrivate::priv, QTextCursorPrivate::remove(), removeSelectedText(), and QTextCursorPrivate::setX().

Referenced by QWidgetTextControlPrivate::keyPressEvent(), and QQuickTextControlPrivate::keyPressEvent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ document()

QTextDocument * QTextCursor::document ( ) const
Since
4.5 Returns the document this cursor is associated with.

Definition at line 2589 of file qtextcursor.cpp.

References QTextDocumentPrivate::document(), and QTextCursorPrivate::priv.

Referenced by QTextMarkdownImporter::cbEnterBlock(), QTextMarkdownImporter::cbText(), and QTextMarkdownImporter::import().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ endEditBlock()

void QTextCursor::endEditBlock ( )

Indicates the end of a block of editing operations on the document that should appear as a single operation from an undo/redo point of view.

See also
beginEditBlock()

Definition at line 2516 of file qtextcursor.cpp.

References QTextDocumentPrivate::endEditBlock(), and QTextCursorPrivate::priv.

Referenced by QWidgetTextControlPrivate::append(), QWidgetTextControlPrivate::commitPreedit(), QWidgetTextControlPrivate::createAutoBulletList(), QWidgetTextControlPrivate::dropEvent(), QTextHtmlImporter::import(), QTextMarkdownImporter::import(), and QWidgetTextControlPrivate::inputMethodEvent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hasComplexSelection()

bool QTextCursor::hasComplexSelection ( ) const

Returns true if the cursor contains a selection that is not simply a range from selectionStart() to selectionEnd(); otherwise returns false.

Complex selections are ones that span at least two cells in a table; their extent is specified by selectedTableCells().

Definition at line 1585 of file qtextcursor.cpp.

References QTextCursorPrivate::complexSelectionTable().

Referenced by QTextCopyHelper::copy(), QWidgetTextControlPrivate::repaintOldAndNewSelection(), and QQuickTextControlPrivate::repaintOldAndNewSelection().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hasSelection()

◆ insertBlock() [1/3]

void QTextCursor::insertBlock ( )

Inserts a new empty block at the cursor position() with the current blockFormat() and charFormat().

See also
setBlockFormat()

Definition at line 1997 of file qtextcursor.cpp.

References blockFormat(), and insertBlock().

Referenced by QWidgetTextControlPrivate::append(), QTextMarkdownImporter::cbEnterBlock(), insertBlock(), insertBlock(), insertList(), insertList(), QWidgetTextControlPrivate::insertParagraphSeparator(), and QQuickTextControlPrivate::keyPressEvent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ insertBlock() [2/3]

void QTextCursor::insertBlock ( const QTextBlockFormat & format)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Inserts a new empty block at the cursor position() with block format format and the current charFormat() as block char format.

See also
setBlockFormat()

Definition at line 2010 of file qtextcursor.cpp.

References charFormat(), insertBlock(), and QTextFormat::ObjectType.

+ Here is the call graph for this function:

◆ insertBlock() [3/3]

void QTextCursor::insertBlock ( const QTextBlockFormat & format,
const QTextCharFormat & charFormat )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Inserts a new empty block at the cursor position() with block format format and charFormat as block char format.

See also
setBlockFormat()

Definition at line 2026 of file qtextcursor.cpp.

References QTextDocumentPrivate::beginEditBlock(), charFormat(), QTextFormat::clearProperty(), QTextDocumentPrivate::endEditBlock(), QTextCursorPrivate::insertBlock(), QTextFormat::ObjectIndex, QTextCursorPrivate::priv, QTextCursorPrivate::remove(), and QTextCursorPrivate::setX().

+ Here is the call graph for this function:

◆ insertFragment()

void QTextCursor::insertFragment ( const QTextDocumentFragment & fragment)

Inserts the text fragment at the current position().

Definition at line 2219 of file qtextcursor.cpp.

References QTextDocumentPrivate::beginEditBlock(), QTextDocumentFragmentPrivate::doc, QTextDocumentPrivate::endEditBlock(), QTextDocumentPrivate::get(), QTextDocumentFragmentPrivate::insert(), QTextDocumentFragment::isEmpty(), QTextDocumentPrivate::mergeCachedResources(), QTextCursorPrivate::priv, QTextCursorPrivate::remove(), and QTextCursorPrivate::setX().

Referenced by QTextDocument::clone(), insertHtml(), and QWidgetTextControl::print().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ insertFrame()

QTextFrame * QTextCursor::insertFrame ( const QTextFrameFormat & format)

Inserts a frame with the given format at the current cursor position(), moves the cursor position() inside the frame, and returns the frame.

If the cursor holds a selection, the whole selection is moved inside the frame.

See also
hasSelection()

Definition at line 2194 of file qtextcursor.cpp.

References QTextDocumentPrivate::insertFrame(), QTextCursorPrivate::priv, selectionEnd(), and selectionStart().

+ Here is the call graph for this function:

◆ insertHtml()

void QTextCursor::insertHtml ( const QString & html)
Since
4.2 Inserts the text html at the current position(). The text is interpreted as HTML.
Note
When using this function with a style sheet, the style sheet will only apply to the current block in the document. In order to apply a style sheet throughout a document, use QTextDocument::setDefaultStyleSheet() instead.

Definition at line 2247 of file qtextcursor.cpp.

References QTextDocumentPrivate::document(), QTextDocumentFragment::fromHtml(), insertFragment(), and QTextCursorPrivate::priv.

Referenced by QWidgetTextControlPrivate::append(), and QTextMarkdownImporter::cbText().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ insertImage() [1/4]

void QTextCursor::insertImage ( const QImage & image,
const QString & name = QString() )
Since
4.5 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Convenience function for inserting the given image with an optional name at the current position().

Definition at line 2337 of file qtextcursor.cpp.

References QTextDocument::addResource(), QTextDocumentPrivate::document(), QTextDocument::ImageResource, insertImage(), QString::number(), QTextCursorPrivate::priv, qWarning, and QTextImageFormat::setName().

+ Here is the call graph for this function:

◆ insertImage() [2/4]

void QTextCursor::insertImage ( const QString & name)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Convenience method for inserting the image with the given name at the current position().

cursor.insertImage("myimage");

Definition at line 2323 of file qtextcursor.cpp.

References insertImage(), and QTextImageFormat::setName().

+ Here is the call graph for this function:

◆ insertImage() [3/4]

void QTextCursor::insertImage ( const QTextImageFormat & format)

Inserts the image defined by format at the current position().

Definition at line 2310 of file qtextcursor.cpp.

References insertText().

+ Here is the call graph for this function:

◆ insertImage() [4/4]

void QTextCursor::insertImage ( const QTextImageFormat & format,
QTextFrameFormat::Position alignment )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
6.4 Inserts the markdown text at the current position(), with the specified Markdown features. The default is GitHub dialect.
4.2

Inserts the image defined by the given format at the cursor's current position with the specified alignment.

See also
position()

Definition at line 2288 of file qtextcursor.cpp.

References alignment, QTextDocumentPrivate::beginEditBlock(), QTextDocumentPrivate::createObject(), QTextDocumentPrivate::endEditBlock(), fmt, QTextDocumentPrivate::formatCollection(), QTextFormatCollection::indexForFormat(), QTextDocumentPrivate::insert(), QTextCursorPrivate::position, QTextCursorPrivate::priv, QTextCursorPrivate::remove(), QTextFormat::setObjectIndex(), and QTextFrameFormat::setPosition().

Referenced by QTextMarkdownImporter::cbText(), insertImage(), and insertImage().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ insertList() [1/2]

QTextList * QTextCursor::insertList ( const QTextListFormat & format)

Inserts a new block at the current position and makes it the first list item of a newly created list with the given format.

Returns the created list.

See also
currentList(), createList(), insertBlock()

Definition at line 2048 of file qtextcursor.cpp.

References createList(), and insertBlock().

Referenced by QTextMarkdownImporter::cbEnterBlock().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ insertList() [2/2]

QTextList * QTextCursor::insertList ( QTextListFormat::Style style)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Inserts a new block at the current position and makes it the first list item of a newly created list with the given style.

Returns the created list.

See also
currentList(), createList(), insertBlock()

Definition at line 2063 of file qtextcursor.cpp.

References createList(), and insertBlock().

+ Here is the call graph for this function:

◆ insertTable() [1/2]

QTextTable * QTextCursor::insertTable ( int rows,
int columns )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Creates a new table with the given number of rows and columns, inserts it at the current cursor position() in the document, and returns the table object.

The cursor is moved to the beginning of the first cell.

There must be at least one row and one column in the table.

See also
currentTable()

Definition at line 2133 of file qtextcursor.cpp.

References insertTable().

+ Here is the call graph for this function:

◆ insertTable() [2/2]

QTextTable * QTextCursor::insertTable ( int rows,
int columns,
const QTextTableFormat & format )

Creates a new table with the given number of rows and columns in the specified format, inserts it at the current cursor position() in the document, and returns the table object.

The cursor is moved to the beginning of the first cell.

There must be at least one row and one column in the table.

See also
currentTable()

Definition at line 2150 of file qtextcursor.cpp.

References QTextCursorPrivate::adjusted_anchor, QTextCursorPrivate::anchor, QTextTablePrivate::createTable(), pos, QTextCursorPrivate::position, QTextCursorPrivate::priv, and QTextCursorPrivate::setPosition().

Referenced by QTextMarkdownImporter::cbEnterBlock(), and insertTable().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ insertText() [1/2]

void QTextCursor::insertText ( const QString & text)

Inserts text at the current position, using the current character format.

If there is a selection, the selection is deleted and replaced by text, for example:

cursor.clearSelection();
cursor.insertText("Hello World");

This clears any existing selection, selects the word at the cursor (i.e. from position() forward), and replaces the selection with the phrase "Hello World".

Any ASCII linefeed characters (\n) in the inserted text are transformed into unicode block separators, corresponding to insertBlock() calls.

See also
charFormat(), hasSelection()

Definition at line 1387 of file qtextcursor.cpp.

References charFormat(), fmt, insertText(), QTextFormat::ObjectType, and text.

Referenced by QTextDocument::QTextDocument(), QWidgetTextControlPrivate::append(), QTextMarkdownImporter::cbText(), insertImage(), insertText(), QWidgetTextControlPrivate::keyPressEvent(), QQuickTextControlPrivate::keyPressEvent(), and QTextDocument::setPlainText().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ insertText() [2/2]

void QTextCursor::insertText ( const QString & text,
const QTextCharFormat & format )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Inserts text at the current position with the given format.

Definition at line 1400 of file qtextcursor.cpp.

References QTextCursorPrivate::anchor, QString::at(), QTextDocumentPrivate::beginEditBlock(), blockFormat(), ch, QTextFormat::clearProperty(), QTextDocumentPrivate::endEditBlock(), QTextDocumentPrivate::formatCollection(), i, QTextFormatCollection::indexForFormat(), QTextDocumentPrivate::insert(), QTextCursorPrivate::insertBlock(), QString::isEmpty(), QTextCharFormat::isValid(), QTextFormat::ObjectIndex, QTextCursorPrivate::position, QTextCursorPrivate::priv, Q_ASSERT, QTextBeginningOfFrame, QTextEndOfFrame, QTextCursorPrivate::remove(), QTextCursorPrivate::setX(), QString::size(), and text.

+ Here is the call graph for this function:

◆ isCopyOf()

bool QTextCursor::isCopyOf ( const QTextCursor & other) const

Returns true if this cursor and other are copies of each other, i.e.

one of them was created as a copy of the other and neither has moved since. This is much stricter than equality.

See also
operator=(), operator==()

Definition at line 2531 of file qtextcursor.cpp.

References other().

+ Here is the call graph for this function:

◆ isNull()

bool QTextCursor::isNull ( ) const

Returns true if the cursor is null; otherwise returns false.

A null cursor is created by the default constructor.

Definition at line 1102 of file qtextcursor.cpp.

References QTextCursorPrivate::priv.

Referenced by QWidgetTextControlPrivate::cursorMoveKeyEvent(), QQuickTextControlPrivate::cursorMoveKeyEvent(), QWidgetTextControlPrivate::cursorRectPlusUnicodeDirectionMarkers(), QWidgetTextControl::find(), QWidgetTextControlPrivate::inputMethodEvent(), QWidgetTextControlPrivate::repaintOldAndNewSelection(), and QQuickTextControlPrivate::repaintOldAndNewSelection().

+ Here is the caller graph for this function:

◆ joinPreviousEditBlock()

void QTextCursor::joinPreviousEditBlock ( )

Like beginEditBlock() indicates the start of a block of editing operations that should appear as a single operation for undo/redo.

However unlike beginEditBlock() it does not start a new block but reverses the previous call to endEditBlock() and therefore makes following operations part of the previous edit block created.

For example:

cursor.beginEditBlock();
cursor.insertText("Hello");
cursor.insertText("World");
cursor.endEditBlock();
// ...
cursor.joinPreviousEditBlock();
cursor.insertText("Hey");
cursor.endEditBlock();

The call to undo() will cause all three insertions to be undone.

See also
beginEditBlock(), endEditBlock()

Definition at line 2500 of file qtextcursor.cpp.

References QTextDocumentPrivate::joinPreviousEditBlock(), and QTextCursorPrivate::priv.

+ Here is the call graph for this function:

◆ keepPositionOnInsert()

bool QTextCursor::keepPositionOnInsert ( ) const
Since
4.7

Returns whether the cursor should keep its current position when text gets inserted at the position of the cursor.

The default is false;

See also
setKeepPositionOnInsert()

Definition at line 1342 of file qtextcursor.cpp.

References QTextCursorPrivate::keepPositionOnInsert.

◆ mergeBlockCharFormat()

void QTextCursor::mergeBlockCharFormat ( const QTextCharFormat & modifier)

Modifies the block char format of the current block (or all blocks that are contained in the selection) with the block format specified by modifier.

See also
setBlockCharFormat()

Definition at line 1843 of file qtextcursor.cpp.

References QTextDocumentPrivate::MergeFormat, QTextCursorPrivate::priv, and QTextCursorPrivate::setBlockCharFormat().

+ Here is the call graph for this function:

◆ mergeBlockFormat()

void QTextCursor::mergeBlockFormat ( const QTextBlockFormat & modifier)

Modifies the block format of the current block (or all blocks that are contained in the selection) with the block format specified by modifier.

See also
setBlockFormat(), blockFormat()

Definition at line 1798 of file qtextcursor.cpp.

References QTextDocumentPrivate::MergeFormat, QTextCursorPrivate::priv, and QTextCursorPrivate::setBlockFormat().

Referenced by createList(), QWidgetTextControlPrivate::indent(), QWidgetTextControlPrivate::keyPressEvent(), QQuickTextControlPrivate::keyPressEvent(), QWidgetTextControlPrivate::outdent(), and QQuickTextSelection::setAlignment().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mergeCharFormat()

void QTextCursor::mergeCharFormat ( const QTextCharFormat & modifier)

Merges the cursor's current character format with the properties described by format modifier.

If the cursor has a selection, this function applies all the properties set in modifier to all the character formats that are part of the selection.

See also
hasSelection(), setCharFormat()

Definition at line 1919 of file qtextcursor.cpp.

References QTextCursorPrivate::anchor, charFormat(), QTextCursorPrivate::currentCharFormat, QTextDocumentPrivate::formatCollection(), QTextFormatCollection::indexForFormat(), QTextDocumentPrivate::MergeFormat, QTextCursorPrivate::position, QTextCursorPrivate::priv, and QTextCursorPrivate::setCharFormat().

+ Here is the call graph for this function:

◆ movePosition()

bool QTextCursor::movePosition ( MoveOperation operation,
MoveMode mode = MoveAnchor,
int n = 1 )

Moves the cursor by performing the given operation n times, using the specified mode, and returns true if all operations were completed successfully; otherwise returns false.

For example, if this function is repeatedly used to seek to the end of the next word, it will eventually fail when the end of the document is reached.

By default, the move operation is performed once (n = 1).

If mode is KeepAnchor, the cursor selects the text it moves over. This is the same effect that the user achieves when they hold down the Shift key and move the cursor with the cursor keys.

See also
setVisualNavigation()

Definition at line 1224 of file qtextcursor.cpp.

References QTextCursorPrivate::anchor, QTextCursorPrivate::block(), End, EndOfLine, QTextBlock::isVisible(), MoveAnchor, QTextCursorPrivate::movePosition(), QTextBlock::next(), position(), QTextCursorPrivate::position, QTextCursorPrivate::priv, QTextCursorPrivate::setPosition(), Start, StartOfLine, and QTextCursorPrivate::visualNavigation.

Referenced by QWidgetTextControlPrivate::activateLinkUnderCursor(), QQuickTextControlPrivate::activateLinkUnderCursor(), QWidgetTextControlPrivate::anchorForCursor(), QWidgetTextControlPrivate::append(), QTextMarkdownImporter::cbLeaveBlock(), QWidgetTextControlPrivate::cursorMoveKeyEvent(), QQuickTextControlPrivate::cursorMoveKeyEvent(), QWidgetTextControlPrivate::extendBlockwiseSelection(), QQuickTextControlPrivate::extendBlockwiseSelection(), QWidgetTextControlPrivate::extendWordwiseSelection(), QQuickTextControlPrivate::extendWordwiseSelection(), QWidgetTextControlPrivate::keyPressEvent(), QQuickTextControlPrivate::keyPressEvent(), QWidgetTextControlPrivate::mousePressEvent(), QQuickTextControlPrivate::mousePressEvent(), QWidgetTextControl::moveCursor(), QQuickTextControl::moveCursor(), and select().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator!=()

bool QTextCursor::operator!= ( const QTextCursor & rhs) const

Returns true if the other cursor is at a different position in the document as this cursor; otherwise returns false.

Definition at line 2358 of file qtextcursor.cpp.

References operator==().

+ Here is the call graph for this function:

◆ operator<()

bool QTextCursor::operator< ( const QTextCursor & rhs) const

Returns true if the other cursor is positioned later in the document than this cursor; otherwise returns false.

Definition at line 2369 of file qtextcursor.cpp.

References QTextCursorPrivate::position, QTextCursorPrivate::priv, and Q_ASSERT_X.

◆ operator<=()

bool QTextCursor::operator<= ( const QTextCursor & rhs) const

Returns true if the other cursor is positioned later or at the same position in the document as this cursor; otherwise returns false.

Definition at line 2389 of file qtextcursor.cpp.

References QTextCursorPrivate::position, QTextCursorPrivate::priv, and Q_ASSERT_X.

◆ operator=()

QTextCursor & QTextCursor::operator= ( const QTextCursor & cursor)

Makes a copy of cursor and assigns it to this QTextCursor.

Note that QTextCursor is an \l{Implicitly Shared Classes}{implicitly shared} class.

Definition at line 1077 of file qtextcursor.cpp.

References cursor.

◆ operator==()

bool QTextCursor::operator== ( const QTextCursor & rhs) const

Returns true if the other cursor is at the same position in the document as this cursor; otherwise returns false.

Definition at line 2408 of file qtextcursor.cpp.

References QTextCursorPrivate::position, and QTextCursorPrivate::priv.

Referenced by operator!=().

+ Here is the caller graph for this function:

◆ operator>()

bool QTextCursor::operator> ( const QTextCursor & rhs) const

Returns true if the other cursor is positioned earlier in the document than this cursor; otherwise returns false.

Definition at line 2445 of file qtextcursor.cpp.

References QTextCursorPrivate::position, QTextCursorPrivate::priv, and Q_ASSERT_X.

◆ operator>=()

bool QTextCursor::operator>= ( const QTextCursor & rhs) const

Returns true if the other cursor is positioned earlier or at the same position in the document as this cursor; otherwise returns false.

Definition at line 2426 of file qtextcursor.cpp.

References QTextCursorPrivate::position, QTextCursorPrivate::priv, and Q_ASSERT_X.

◆ position()

int QTextCursor::position ( ) const

Returns the absolute position of the cursor within the document.

The cursor is positioned between characters.

Note
The "characters" in this case refer to the string of QChar objects, i.e. 16-bit Unicode characters, and the position is considered an index into this string. This does not necessarily correspond to individual graphemes in the writing system, as a single grapheme may be represented by multiple Unicode characters, such as in the case of surrogate pairs, linguistic ligatures or diacritics.
See also
setPosition(), movePosition(), anchor(), positionInBlock()

Definition at line 1161 of file qtextcursor.cpp.

References QTextCursorPrivate::position, and QTextCursorPrivate::priv.

Referenced by QWidgetTextControlPrivate::activateLinkUnderCursor(), QQuickTextControlPrivate::activateLinkUnderCursor(), QWidgetTextControlPrivate::anchorForCursor(), QWidgetTextControlPrivate::cursorMoveKeyEvent(), QQuickTextControlPrivate::cursorMoveKeyEvent(), QWidgetTextControlPrivate::cursorRectPlusUnicodeDirectionMarkers(), QPlainTextEditPrivate::ensureCursorVisible(), QWidgetTextControlPrivate::extendBlockwiseSelection(), QQuickTextControlPrivate::extendBlockwiseSelection(), QWidgetTextControlPrivate::extendWordwiseSelection(), QQuickTextControlPrivate::extendWordwiseSelection(), QWidgetTextControlPrivate::inputMethodEvent(), QWidgetTextControl::inputMethodQuery(), QWidgetTextControlPrivate::keyPressEvent(), QQuickTextControlPrivate::keyPressEvent(), QWidgetTextControlPrivate::mouseMoveEvent(), QQuickTextControlPrivate::mouseMoveEvent(), QWidgetTextControlPrivate::mousePressEvent(), QQuickTextControlPrivate::mousePressEvent(), QWidgetTextControlPrivate::mouseReleaseEvent(), QQuickTextControlPrivate::mouseReleaseEvent(), movePosition(), QWidgetTextControlPrivate::repaintOldAndNewSelection(), QQuickTextControlPrivate::repaintOldAndNewSelection(), QWidgetTextControlPrivate::selectionChanged(), QWidgetTextControlPrivate::sendMouseEventToInputContext(), QQuickTextControlPrivate::sendMouseEventToInputContext(), and QQuickTextControlPrivate::setContent().

+ Here is the caller graph for this function:

◆ positionInBlock()

int QTextCursor::positionInBlock ( ) const
Since
4.7 Returns the relative position of the cursor within the block. The cursor is positioned between characters.

This is equivalent to { position() - block().position()}.

Note
The "characters" in this case refer to the string of QChar objects, i.e. 16-bit Unicode characters, and the position is considered an index into this string. This does not necessarily correspond to individual graphemes in the writing system, as a single grapheme may be represented by multiple Unicode characters, such as in the case of surrogate pairs, linguistic ligatures or diacritics.
See also
position()

Definition at line 1184 of file qtextcursor.cpp.

References QTextCursorPrivate::block(), QTextCursorPrivate::position, QTextBlock::position(), and QTextCursorPrivate::priv.

+ Here is the call graph for this function:

◆ removeSelectedText()

void QTextCursor::removeSelectedText ( )

If there is a selection, its content is deleted; otherwise does nothing.

See also
hasSelection()

Definition at line 1636 of file qtextcursor.cpp.

References QTextCursorPrivate::anchor, QTextDocumentPrivate::beginEditBlock(), QTextDocumentPrivate::endEditBlock(), QTextCursorPrivate::position, QTextCursorPrivate::priv, QTextCursorPrivate::remove(), and QTextCursorPrivate::setX().

Referenced by QWidgetTextControlPrivate::_q_deleteSelected(), deleteChar(), deletePreviousChar(), QWidgetTextControlPrivate::dropEvent(), QWidgetTextControlPrivate::inputMethodEvent(), QWidgetTextControlPrivate::keyPressEvent(), QQuickTextControlPrivate::keyPressEvent(), and QWidgetTextControlPrivate::startDrag().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ select()

void QTextCursor::select ( SelectionType selection)

Selects text in the document according to the given selection.

Definition at line 1533 of file qtextcursor.cpp.

References block(), QTextCursorPrivate::block(), BlockUnderCursor, clearSelection(), Document, End, EndOfBlock, EndOfLine, EndOfWord, KeepAnchor, QTextBlock::length(), LineUnderCursor, movePosition(), NextBlock, PreviousBlock, QTextCursorPrivate::priv, selection(), Start, StartOfBlock, StartOfLine, StartOfWord, and WordUnderCursor.

Referenced by QWidgetTextControlPrivate::mouseDoubleClickEvent(), QQuickTextControlPrivate::mouseDoubleClickEvent(), QWidgetTextControlPrivate::mouseMoveEvent(), QQuickTextControlPrivate::mouseMoveEvent(), QWidgetTextControlPrivate::mousePressEvent(), and QQuickTextControlPrivate::mousePressEvent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ selectedTableCells()

void QTextCursor::selectedTableCells ( int * firstRow,
int * numRows,
int * firstColumn,
int * numColumns ) const

If the selection spans over table cells, firstRow is populated with the number of the first row in the selection, firstColumn with the number of the first column in the selection, and numRows and numColumns with the number of rows and columns in the selection.

If the selection does not span any table cells the results are harmless but undefined.

Definition at line 1601 of file qtextcursor.cpp.

References QTextCursorPrivate::anchor, QTextCursorPrivate::position, and QTextCursorPrivate::selectedTableCells().

Referenced by QTextCopyHelper::copy(), and selectedText().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ selectedText()

QString QTextCursor::selectedText ( ) const

Returns the current selection's text (which may be empty).

This only returns the text, with no rich text formatting information. If you want a document fragment (i.e. formatted rich text) use selection() instead.

Note
If the selection obtained from an editor spans a line break, the text will contain a Unicode U+2029 paragraph separator character instead of a newline {
} character. Use QString::replace() to replace these characters with newlines.

Definition at line 1698 of file qtextcursor.cpp.

References QTextCursorPrivate::anchor, QTextDocumentPrivate::buffer(), QTextTableCell::column(), QTextTableCell::columnSpan(), QTextCursorPrivate::complexSelectionTable(), QTextTableCell::firstPosition(), getText(), QTextTableCell::lastPosition(), QTextCursorPrivate::position, QTextCursorPrivate::priv, Q_ASSERT, QTextTableCell::row(), QTextTableCell::rowSpan(), selectedTableCells(), selectionEnd(), selectionStart(), and text.

Referenced by QWidgetTextControlPrivate::_q_contentsChanged(), and QQuickTextSelection::text().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ selection()

QTextDocumentFragment QTextCursor::selection ( ) const

Returns the current selection (which may be empty) with all its formatting information.

If you just want the selected text (i.e. plain text) use selectedText() instead.

Note
Unlike QTextDocumentFragment::toPlainText(), selectedText() may include special unicode characters such as QChar::ParagraphSeparator.
See also
QTextDocumentFragment::toPlainText()

Definition at line 1749 of file qtextcursor.cpp.

Referenced by select().

+ Here is the caller graph for this function:

◆ selectionEnd()

int QTextCursor::selectionEnd ( ) const

Returns the end of the selection or position() if the cursor doesn't have a selection.

See also
selectionStart(), position(), anchor()

Definition at line 1666 of file qtextcursor.cpp.

References QTextCursorPrivate::adjusted_anchor, QTextCursorPrivate::position, QTextCursorPrivate::priv, and qMax().

Referenced by QTextCopyHelper::copy(), QWidgetTextControlPrivate::extendBlockwiseSelection(), QQuickTextControlPrivate::extendBlockwiseSelection(), QWidgetTextControlPrivate::extendWordwiseSelection(), QQuickTextControlPrivate::extendWordwiseSelection(), insertFrame(), QWidgetTextControlPrivate::mousePressEvent(), selectedText(), and QQuickTextControlPrivate::selectionChanged().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ selectionStart()

int QTextCursor::selectionStart ( ) const

Returns the start of the selection or position() if the cursor doesn't have a selection.

See also
selectionEnd(), position(), anchor()

Definition at line 1653 of file qtextcursor.cpp.

References QTextCursorPrivate::adjusted_anchor, QTextCursorPrivate::position, QTextCursorPrivate::priv, and qMin().

Referenced by QWidgetTextControlPrivate::activateLinkUnderCursor(), QQuickTextControlPrivate::activateLinkUnderCursor(), QWidgetTextControlPrivate::anchorForCursor(), QTextCopyHelper::copy(), QWidgetTextControlPrivate::extendBlockwiseSelection(), QQuickTextControlPrivate::extendBlockwiseSelection(), QWidgetTextControlPrivate::extendWordwiseSelection(), QQuickTextControlPrivate::extendWordwiseSelection(), insertFrame(), QWidgetTextControlPrivate::mousePressEvent(), selectedText(), and QQuickTextControlPrivate::selectionChanged().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setBlockCharFormat()

void QTextCursor::setBlockCharFormat ( const QTextCharFormat & format)

Sets the block char format of the current block (or all blocks that are contained in the selection) to format.

See also
blockCharFormat()

Definition at line 1828 of file qtextcursor.cpp.

References QTextCursorPrivate::priv, QTextCursorPrivate::setBlockCharFormat(), and QTextDocumentPrivate::SetFormatAndPreserveObjectIndices.

Referenced by QTextDocument::clone().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setBlockFormat()

void QTextCursor::setBlockFormat ( const QTextBlockFormat & format)

Sets the block format of the current block (or all blocks that are contained in the selection) to format.

See also
blockFormat(), mergeBlockFormat()

Definition at line 1783 of file qtextcursor.cpp.

References QTextCursorPrivate::priv, QTextCursorPrivate::setBlockFormat(), and QTextDocumentPrivate::SetFormat.

Referenced by QTextMarkdownImporter::cbEnterBlock(), QTextMarkdownImporter::cbText(), QTextDocument::clone(), QWidgetTextControlPrivate::createAutoBulletList(), QTextHtmlImporter::import(), QWidgetTextControlPrivate::insertParagraphSeparator(), QWidgetTextControlPrivate::keyPressEvent(), QQuickTextControlPrivate::keyPressEvent(), QWidgetTextControlPrivate::mouseReleaseEvent(), and QQuickTextControlPrivate::mouseReleaseEvent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setCharFormat()

void QTextCursor::setCharFormat ( const QTextCharFormat & format)

Sets the cursor's current character format to the given format.

If the cursor has a selection, the given format is applied to the current selection.

See also
hasSelection(), mergeCharFormat()

Definition at line 1900 of file qtextcursor.cpp.

References QTextCursorPrivate::anchor, QTextCursorPrivate::currentCharFormat, QTextDocumentPrivate::formatCollection(), QTextFormatCollection::indexForFormat(), QTextCursorPrivate::position, QTextCursorPrivate::priv, QTextCursorPrivate::setCharFormat(), and QTextDocumentPrivate::SetFormatAndPreserveObjectIndices.

Referenced by QWidgetTextControlPrivate::append(), QTextMarkdownImporter::cbEnterBlock(), QTextMarkdownImporter::cbEnterSpan(), QTextMarkdownImporter::cbLeaveBlock(), QTextMarkdownImporter::cbLeaveSpan(), QTextMarkdownImporter::cbText(), QWidgetTextControlPrivate::insertParagraphSeparator(), main(), QQuickTextControlPrivate::setContent(), and QWidgetTextControlPrivate::setContent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setKeepPositionOnInsert()

void QTextCursor::setKeepPositionOnInsert ( bool b)
Since
4.7

Defines whether the cursor should keep its current position when text gets inserted at the current position of the cursor.

If b is true, the cursor keeps its current position when text gets inserted at the positing of the cursor. If b is false, the cursor moves along with the inserted text.

The default is false.

Note that a cursor always moves when text is inserted before the current position of the cursor, and it always keeps its position when text is inserted after the current position of the cursor.

See also
keepPositionOnInsert()

Definition at line 1363 of file qtextcursor.cpp.

References QTextCursorPrivate::keepPositionOnInsert.

◆ setPosition()

void QTextCursor::setPosition ( int pos,
MoveMode m = MoveAnchor )

Moves the cursor to the absolute position in the document specified by pos using a MoveMode specified by m.

The cursor is positioned between characters.

Note
The "characters" in this case refer to the string of QChar objects, i.e. 16-bit Unicode characters, and pos is considered an index into this string. This does not necessarily correspond to individual graphemes in the writing system, as a single grapheme may be represented by multiple Unicode characters, such as in the case of surrogate pairs, linguistic ligatures or diacritics. For a more generic approach to navigating the document, use movePosition(), which will respect the actual grapheme boundaries in the text.
See also
position(), movePosition(), anchor()

Definition at line 1123 of file qtextcursor.cpp.

References QTextCursorPrivate::adjustCursor(), QTextCursorPrivate::adjusted_anchor, anchor(), QTextCursorPrivate::anchor, Left, QTextDocumentPrivate::length(), MoveAnchor, pos, QTextCursorPrivate::priv, qWarning, Right, QTextCursorPrivate::setPosition(), and QTextCursorPrivate::setX().

Referenced by QWidgetTextControlPrivate::_q_contentsChanged(), QWidgetTextControlPrivate::activateLinkUnderCursor(), QQuickTextControlPrivate::activateLinkUnderCursor(), QWidgetTextControlPrivate::anchorForCursor(), QWidgetTextControlPrivate::dragMoveEvent(), QWidgetTextControlPrivate::extendBlockwiseSelection(), QQuickTextControlPrivate::extendBlockwiseSelection(), QWidgetTextControlPrivate::extendWordwiseSelection(), QQuickTextControlPrivate::extendWordwiseSelection(), QWidgetTextControlPrivate::inputMethodEvent(), main(), QWidgetTextControlPrivate::mouseReleaseEvent(), QQuickTextControlPrivate::mouseReleaseEvent(), QWidgetTextControlPrivate::setCursorPosition(), QQuickTextControlPrivate::setCursorPosition(), QWidgetTextControlPrivate::setCursorPosition(), and QQuickTextControlPrivate::setCursorPosition().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setVerticalMovementX()

void QTextCursor::setVerticalMovementX ( int x)
Since
4.7

Sets the visual x position for vertical cursor movements to x.

The vertical movement x position is cleared automatically when the cursor moves horizontally, and kept unchanged when the cursor moves vertically. The mechanism allows the cursor to move up and down on a visually straight line with proportional fonts, and to gently "jump" over short lines.

A value of -1 indicates no predefined x position. It will then be set automatically the next time the cursor moves up or down.

See also
verticalMovementX()

Definition at line 1312 of file qtextcursor.cpp.

References QTextCursorPrivate::x.

◆ setVisualNavigation()

void QTextCursor::setVisualNavigation ( bool b)
Since
4.4

Sets visual navigation to b.

Visual navigation means skipping over hidden text paragraphs. The default is false.

See also
visualNavigation(), movePosition()

Definition at line 1291 of file qtextcursor.cpp.

References QTextCursorPrivate::visualNavigation.

Referenced by QWidgetTextControlPrivate::cursorMoveKeyEvent(), and QQuickTextControlPrivate::cursorMoveKeyEvent().

+ Here is the caller graph for this function:

◆ swap()

void QTextCursor::swap ( QTextCursor & other)
inlinenoexcept
Since
5.0

Swaps this text cursor instance with other. This function is very fast and never fails.

Definition at line 43 of file qtextcursor.h.

References d, and other().

+ Here is the call graph for this function:

◆ verticalMovementX()

int QTextCursor::verticalMovementX ( ) const
Since
4.7

Returns the visual x position for vertical cursor movements.

A value of -1 indicates no predefined x position. It will then be set automatically the next time the cursor moves up or down.

See also
setVerticalMovementX()

Definition at line 1327 of file qtextcursor.cpp.

References QTextCursorPrivate::x.

◆ visualNavigation()

bool QTextCursor::visualNavigation ( ) const
Since
4.4

Returns true if the cursor does visual navigation; otherwise returns false.

Visual navigation means skipping over hidden text paragraphs. The default is false.

See also
setVisualNavigation(), movePosition()

Definition at line 1276 of file qtextcursor.cpp.

References QTextCursorPrivate::visualNavigation.

Referenced by QWidgetTextControlPrivate::cursorMoveKeyEvent(), and QQuickTextControlPrivate::cursorMoveKeyEvent().

+ Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ QTextCopyHelper

friend class QTextCopyHelper
friend

Definition at line 200 of file qtextcursor.h.

◆ QTextCursorPrivate

friend class QTextCursorPrivate
friend

Definition at line 197 of file qtextcursor.h.

◆ QTextDocumentFragmentPrivate

friend class QTextDocumentFragmentPrivate
friend

Definition at line 199 of file qtextcursor.h.

◆ QTextDocumentPrivate

friend class QTextDocumentPrivate
friend

Definition at line 198 of file qtextcursor.h.

◆ QWidgetTextControlPrivate

friend class QWidgetTextControlPrivate
friend

Definition at line 201 of file qtextcursor.h.


The documentation for this class was generated from the following files: