Skip to content

PythonQt.Qt.QTextCursor

QTextCursor

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

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.

Source: https://doc.qt.io/archives/qt-5.15/qtextcursor.html

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
BlockUnderCursor attribute Any (shared captured text)
Document attribute Any (shared captured text)
Down attribute Any (shared captured text)
End attribute Any (shared captured text)
EndOfBlock attribute Any (shared captured text)
EndOfLine attribute Any (shared captured text)
EndOfWord attribute Any (shared captured text)
KeepAnchor attribute Any (shared captured text)
Left attribute Any (shared captured text)
LineUnderCursor attribute Any (shared captured text)
MoveAnchor attribute Any (shared captured text)
MoveMode attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
MoveOperation attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
NextBlock attribute Any (shared captured text)
NextCell attribute Any (shared captured text)
NextCharacter attribute Any (shared captured text)
NextRow attribute Any (shared captured text)
NextWord attribute Any (shared captured text)
NoMove attribute Any (shared captured text)
PreviousBlock attribute Any (shared captured text)
PreviousCell attribute Any (shared captured text)
PreviousCharacter attribute Any (shared captured text)
PreviousRow attribute Any (shared captured text)
PreviousWord attribute Any (shared captured text)
Right attribute Any (shared captured text)
SelectionType attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
Start attribute Any (shared captured text)
StartOfBlock attribute Any (shared captured text)
StartOfLine attribute Any (shared captured text)
StartOfWord attribute Any (shared captured text)
Up attribute Any (shared captured text)
WordLeft attribute Any (shared captured text)
WordRight attribute Any (shared captured text)
WordUnderCursor attribute Any (shared captured text)
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
anchor function (self) -> int X.anchor() -> int (shared captured text)
atBlockEnd function (self) -> bool X.atBlockEnd() -> bool (shared captured text)
atBlockStart function (self) -> bool X.atBlockStart() -> bool (shared captured text)
atEnd function (self) -> bool X.atEnd() -> bool (shared captured text)
atStart function (self) -> bool X.atStart() -> bool (shared captured text)
beginEditBlock function (self) -> None 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. (shared captured text)
block function (self) -> Any X.block() -> PythonQt.QtGui.QTextBlock (shared captured text)
blockCharFormat function (self) -> Any X.blockCharFormat() -> PythonQt.QtGui.QTextCharFormat (shared captured text)
blockFormat function (self) -> Any X.blockFormat() -> PythonQt.QtGui.QTextBlockFormat (shared captured text)
blockNumber function (self) -> int X.blockNumber() -> int (shared captured text)
charFormat function (self) -> Any X.charFormat() -> PythonQt.QtGui.QTextCharFormat (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
clearSelection function (self) -> None Clears the current selection by setting the anchor to the cursor position. (shared captured text)
columnNumber function (self) -> int X.columnNumber() -> int (shared captured text)
createList function (self, style: Any) -> Any X.createList(style) -> PythonQt.QtGui.QTextList (shared captured text)
currentFrame function (self) -> Any X.currentFrame() -> PythonQt.QtGui.QTextFrame (shared captured text)
currentList function (self) -> Any X.currentList() -> PythonQt.QtGui.QTextList (shared captured text)
currentTable function (self) -> Any X.currentTable() -> PythonQt.QtGui.QTextTable (shared captured text)
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
deleteChar function (self) -> None If there is no selected text, deletes the character at the current cursor position; otherwise deletes the selected text. (shared captured text)
deletePreviousChar function (self) -> None If there is no selected text, deletes the character before the current cursor position; otherwise deletes the selected text. (shared captured text)
document function (self) -> Any X.document() -> PythonQt.QtGui.QTextDocument (shared captured text)
endEditBlock function (self) -> None 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. (shared captured text)
hasComplexSelection function (self) -> bool X.hasComplexSelection() -> bool (shared captured text)
hasSelection function (self) -> bool X.hasSelection() -> bool (shared captured text)
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
inherits function (self, *args: Any, **kwargs: Any) -> Any Returns if the class inherits or is of given type name (shared captured text)
insertBlock function (self, format: Any, charFormat: Any) -> None X.insertBlock(format, charFormat) (shared captured text)
insertFragment function (self, fragment: Any) -> None Inserts the text fragment at the current [position](https://doc.qt.io/archives/qt-5.15/qtextcursor.html#position)(). (shared captured text)
insertFrame function (self, format: Any) -> Any X.insertFrame(format) -> PythonQt.QtGui.QTextFrame (shared captured text)
insertHtml function (self, html: str) -> None Inserts the text html at the current [position](https://doc.qt.io/archives/qt-5.15/qtextcursor.html#position)(). (shared captured text)
insertImage function (self, image: Any, name: Any) -> None X.insertImage(image, name) (shared captured text)
insertList function (self, style: Any) -> Any X.insertList(style) -> PythonQt.QtGui.QTextList (shared captured text)
insertTable function (self, rows: int, cols: int, format: Any) -> Any X.insertTable(rows, cols, format) -> PythonQt.QtGui.QTextTable (shared captured text)
insertText function (self, text: str, format: Any) -> None X.insertText(text, format) (shared captured text)
isCopyOf function (self, other: Any) -> bool X.isCopyOf(other) -> bool (shared captured text)
isNull function (self) -> bool X.isNull() -> bool (shared captured text)
joinPreviousEditBlock function (self) -> None Like [beginEditBlock](https://doc.qt.io/archives/qt-5.15/qtextcursor.html#beginEditBlock)() indicates the start of a block of editing operations that should appear as a single operation for undo/redo. (shared captured text)
keepPositionOnInsert function (self) -> bool X.keepPositionOnInsert() -> bool (shared captured text)
mergeBlockCharFormat function (self, modifier: Any) -> None 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. (shared captured text)
mergeBlockFormat function (self, modifier: Any) -> None Modifies the block format of the current block (or all blocks that are contained in the selection) with the block format specified by modifier. (shared captured text)
mergeCharFormat function (self, modifier: Any) -> None Merges the cursor's current character format with the properties described by format modifier. (shared captured text)
movePosition function (self, op: Any, arg__2: Any, n: int) -> bool X.movePosition(op, arg__2, n) -> bool (shared captured text)
position function (self) -> int X.position() -> int (shared captured text)
positionInBlock function (self) -> int X.positionInBlock() -> int (shared captured text)
removeSelectedText function (self) -> None If there is a selection, its content is deleted; otherwise does nothing. (shared captured text)
select function (self, selection: Any) -> None Selects text in the document according to the given selection. (shared captured text)
selectedText function (self) -> str X.selectedText() -> str (shared captured text)
selection function (self) -> Any X.selection() -> PythonQt.QtGui.QTextDocumentFragment (shared captured text)
selectionEnd function (self) -> int X.selectionEnd() -> int (shared captured text)
selectionStart function (self) -> int X.selectionStart() -> int (shared captured text)
setBlockCharFormat function (self, format: Any) -> None Sets the block char format of the current block (or all blocks that are contained in the selection) to format. (shared captured text)
setBlockFormat function (self, format: Any) -> None Sets the block format of the current block (or all blocks that are contained in the selection) to format. (shared captured text)
setCharFormat function (self, format: Any) -> None Sets the cursor's current character format to the given format. (shared captured text)
setKeepPositionOnInsert function (self, b: bool) -> None Defines whether the cursor should keep its current position when text gets inserted at the current position of the cursor. (shared captured text)
setPosition function (self, pos: int, mode: Any) -> None X.setPosition(pos, mode) (shared captured text)
setVerticalMovementX function (self, x: int) -> None Sets the visual x position for vertical cursor movements to x. (shared captured text)
setVisualNavigation function (self, b: bool) -> None Sets visual navigation to b. (shared captured text)
swap function (self, other: Any) -> None Swaps this text cursor instance with other. (shared captured text)
verticalMovementX function (self) -> int X.verticalMovementX() -> int (shared captured text)
visualNavigation function (self) -> bool X.visualNavigation() -> bool (shared captured text)