Skip to content

PythonQt.QtGui.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
Document attribute Any
Down attribute Any (shared captured text)
End attribute Any (shared captured text)
EndOfBlock attribute Any
EndOfLine attribute Any
EndOfWord attribute Any
KeepAnchor attribute Any
Left attribute Any (shared captured text)
LineUnderCursor attribute Any
MoveAnchor attribute Any
MoveMode attribute type Convert a number or string to an integer, or return 0 if no arguments are given.
MoveOperation attribute type Convert a number or string to an integer, or return 0 if no arguments are given.
NextBlock attribute Any
NextCell attribute Any
NextCharacter attribute Any
NextRow attribute Any
NextWord attribute Any
NoMove attribute Any
PreviousBlock attribute Any
PreviousCell attribute Any
PreviousCharacter attribute Any
PreviousRow attribute Any
PreviousWord attribute Any
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.
Start attribute Any
StartOfBlock attribute Any
StartOfLine attribute Any
StartOfWord attribute Any
Up attribute Any (shared captured text)
WordLeft attribute Any
WordRight attribute Any
WordUnderCursor attribute Any
__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
atBlockEnd function (self) -> bool X.atBlockEnd() -> bool
atBlockStart function (self) -> bool X.atBlockStart() -> bool
atEnd function (self) -> bool X.atEnd() -> bool
atStart function (self) -> bool X.atStart() -> bool
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.
block function (self) -> Any X.block() -> PythonQt.QtGui.QTextBlock
blockCharFormat function (self) -> Any X.blockCharFormat() -> PythonQt.QtGui.QTextCharFormat
blockFormat function (self) -> Any X.blockFormat() -> PythonQt.QtGui.QTextBlockFormat
blockNumber function (self) -> int X.blockNumber() -> int
charFormat function (self) -> Any X.charFormat() -> PythonQt.QtGui.QTextCharFormat
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.
columnNumber function (self) -> int X.columnNumber() -> int
createList function (self, style: Any) -> Any X.createList(style) -> PythonQt.QtGui.QTextList
currentFrame function (self) -> Any X.currentFrame() -> PythonQt.QtGui.QTextFrame
currentList function (self) -> Any X.currentList() -> PythonQt.QtGui.QTextList
currentTable function (self) -> Any X.currentTable() -> PythonQt.QtGui.QTextTable
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.
deletePreviousChar function (self) -> None If there is no selected text, deletes the character before the current cursor position; otherwise deletes the selected text.
document function (self) -> Any X.document() -> PythonQt.QtGui.QTextDocument
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.
hasComplexSelection function (self) -> bool X.hasComplexSelection() -> bool
hasSelection function (self) -> bool X.hasSelection() -> bool
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)
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)().
insertFrame function (self, format: Any) -> Any X.insertFrame(format) -> PythonQt.QtGui.QTextFrame
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)().
insertImage function (self, image: Any, name: Any) -> None X.insertImage(image, name)
insertList function (self, style: Any) -> Any X.insertList(style) -> PythonQt.QtGui.QTextList
insertTable function (self, rows: int, cols: int, format: Any) -> Any X.insertTable(rows, cols, format) -> PythonQt.QtGui.QTextTable
insertText function (self, text: str, format: Any) -> None X.insertText(text, format)
isCopyOf function (self, other: Any) -> bool X.isCopyOf(other) -> bool
isNull function (self) -> bool X.isNull() -> bool
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.
keepPositionOnInsert function (self) -> bool X.keepPositionOnInsert() -> bool
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.
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.
mergeCharFormat function (self, modifier: Any) -> None Merges the cursor's current character format with the properties described by format modifier.
movePosition function (self, op: Any, arg__2: Any, n: int) -> bool X.movePosition(op, arg__2, n) -> bool
position function (self) -> int X.position() -> int
positionInBlock function (self) -> int X.positionInBlock() -> int
removeSelectedText function (self) -> None If there is a selection, its content is deleted; otherwise does nothing.
select function (self, selection: Any) -> None Selects text in the document according to the given selection.
selectedText function (self) -> str X.selectedText() -> str
selection function (self) -> Any X.selection() -> PythonQt.QtGui.QTextDocumentFragment
selectionEnd function (self) -> int X.selectionEnd() -> int
selectionStart function (self) -> int X.selectionStart() -> int
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.
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.
setCharFormat function (self, format: Any) -> None Sets the cursor's current character format to the given format.
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.
setPosition function (self, pos: int, mode: Any) -> None X.setPosition(pos, mode)
setVerticalMovementX function (self, x: int) -> None Sets the visual x position for vertical cursor movements to x.
setVisualNavigation function (self, b: bool) -> None Sets visual navigation to b.
swap function (self, other: Any) -> None Swaps this text cursor instance with other.
verticalMovementX function (self) -> int X.verticalMovementX() -> int
visualNavigation function (self) -> bool X.visualNavigation() -> bool