内容表

上一话题

QTextCharFormat

下一话题

QTextDocument

QTextCursor

QTextCursor class offers an API to access and modify QTextDocuments. 更多

Inheritance diagram of PySide2.QtGui.QTextCursor

概要

函数

详细描述

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 及它所做的任何选择。

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() position() is the selection. If anchor() == position() there is no selection.

光标位置可以通过编程方式被改变,使用 setPosition() and movePosition() ; the latter can also be used to select text. For selections see selectionStart() , selectionEnd() , hasSelection() , clearSelection() ,和 removeSelectedText() .

position() is at the start of a block, atBlockStart() 返回 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() .

格式化可以被应用到当前文本文档使用 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() .

删除可以达成使用 deleteChar() , deletePreviousChar() ,和 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.

另请参阅

富文本处理

class QTextCursor

QTextCursor(document)

QTextCursor(frame)

QTextCursor(block)

QTextCursor(cursor)

param frame

QTextFrame

param block

QTextBlock

param document

QTextDocument

param cursor

QTextCursor

构造 null 光标。

构造指向起始的光标为 document .

构造指向起始的光标为 frame .

PySide2.QtGui.QTextCursor. MoveMode

常量

描述

QTextCursor.MoveAnchor

将锚点移到如光标自身的相同位置。

QTextCursor.KeepAnchor

保持锚点位置。

anchor() is kept where it is and the position() is moved, the text in between will be selected.

PySide2.QtGui.QTextCursor. MoveOperation

常量

描述

QTextCursor.NoMove

保持光标位置

QTextCursor.Start

移至文档开头。

QTextCursor.StartOfLine

移至当前行开头。

QTextCursor.StartOfBlock

移至当前块开头。

QTextCursor.StartOfWord

移至当前单词开头。

QTextCursor.PreviousBlock

移至先前块开头。

QTextCursor.PreviousCharacter

移至上一字符。

QTextCursor.PreviousWord

移至先前单词开头。

QTextCursor.Up

上移一行。

QTextCursor.Left

左移一字符。

QTextCursor.WordLeft

左移一单词。

QTextCursor.End

移至文档末尾。

QTextCursor.EndOfLine

移至当前行末尾。

QTextCursor.EndOfWord

移至当前单词末尾。

QTextCursor.EndOfBlock

移至当前块末尾。

QTextCursor.NextBlock

移至下一块起始。

QTextCursor.NextCharacter

移至下一字符。

QTextCursor.NextWord

移至下一单词。

QTextCursor.Down

下移一行。

QTextCursor.Right

右移一字符。

QTextCursor.WordRight

右移一单词。

QTextCursor.NextCell

移到当前表格下一表格单元格的起始。若当前单元格是行的最后单元格,则光标移到下一行的第一单元格。

QTextCursor.PreviousCell

移到当前表格上一表格单元格的起始。若当前单元格是行的第一单元格,则光标将移到上一行的最后单元格。

QTextCursor.NextRow

移到当前表格下一行的第一新单元格。

QTextCursor.PreviousRow

移到当前表格上一行的最后单元格。

另请参阅

movePosition()

PySide2.QtGui.QTextCursor. SelectionType

此枚举描述的选定类型可应用于 select() 函数。

常量

描述

QTextCursor.Document

选择整个文档。

QTextCursor.BlockUnderCursor

选择光标下的文本块。

QTextCursor.LineUnderCursor

选择光标下的文本行。

QTextCursor.WordUnderCursor

选择光标下的单词。若光标不在可选字符的字符串内,则不选择文本。

PySide2.QtGui.QTextCursor. anchor ( )
返回类型

int

返回锚点位置;这如同 position() unless there is a selection in which case position() marks one end of the selection and marks the other end. Just like the cursor position, the anchor position is between characters.

PySide2.QtGui.QTextCursor. atBlockEnd ( )
返回类型

bool

返回 true 若光标在块末尾;否则返回 false .

PySide2.QtGui.QTextCursor. atBlockStart ( )
返回类型

bool

返回 true 若光标在块开头;否则返回 false .

PySide2.QtGui.QTextCursor. atEnd ( )
返回类型

bool

返回 true 若光标在文档末尾;否则返回 false .

PySide2.QtGui.QTextCursor. atStart ( )
返回类型

bool

返回 true 若光标在文档开头;否则返回 false .

PySide2.QtGui.QTextCursor. beginEditBlock ( )

开始在文档中编辑操作块的指示,这应该作为单一操作出现 (从撤销/重做角度来看)。

例如:

cursor = QTextCursor(textDocument)
cursor.beginEditBlock()
cursor.insertText("Hello")
cursor.insertText("World")
cursor.endEditBlock()
textDocument.undo()
											

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 and endEditBlock 。最上面的一对将确定撤消/重做操作的作用域。

另请参阅

endEditBlock()

PySide2.QtGui.QTextCursor. block ( )
返回类型

QTextBlock

返回包含光标的块。

PySide2.QtGui.QTextCursor. blockCharFormat ( )
返回类型

QTextCharFormat

返回光标所在块的块字符格式。

块字符格式是所用格式,当在空块开头插入文本时。

PySide2.QtGui.QTextCursor. blockFormat ( )
返回类型

QTextBlockFormat

返回光标所在块的块格式。

PySide2.QtGui.QTextCursor. blockNumber ( )
返回类型

int

返回光标所在块的编号,或 0 若光标无效。

注意:此函数在文档中才有意义,没有复杂对象 (譬如:表格或框架)。

PySide2.QtGui.QTextCursor. charFormat ( )
返回类型

QTextCharFormat

立即返回光标之前的字符格式,在 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.

PySide2.QtGui.QTextCursor. clearSelection ( )

清零当前选定,通过把锚点设置到光标位置。

Note that it does not 删除选定的文本。

PySide2.QtGui.QTextCursor. columnNumber ( )
返回类型

int

返回在其包含行内的光标位置。

注意:这是相对于换行而不是相对于块 (即:段落) 的列号。

可能想要调用 positionInBlock() 代替。

另请参阅

positionInBlock()

PySide2.QtGui.QTextCursor. createList ( style )
参数

style Style

返回类型

QTextList

这是重载函数。

创建并返回新列表采用给定 style , making the cursor’s current paragraph the first list item.

定义要使用样式通过 Style 枚举。

PySide2.QtGui.QTextCursor. createList ( format )
参数

format QTextListFormat

返回类型

QTextList

PySide2.QtGui.QTextCursor. currentFrame ( )
返回类型

QTextFrame

返回当前框架指针。返回 None 若光标无效。

另请参阅

insertFrame()

PySide2.QtGui.QTextCursor. currentList ( )
返回类型

QTextList

返回当前列表若光标 position() is inside a block that is part of a list; otherwise returns None .

PySide2.QtGui.QTextCursor. currentTable ( )
返回类型

QTextTable

返回当前表格指针若光标 position() is inside a block that is part of a table; otherwise returns None .

另请参阅

insertTable()

PySide2.QtGui.QTextCursor. deleteChar ( )

若没有选中文本,删除字符 at 当前光标位置;否则删除选中文本。

PySide2.QtGui.QTextCursor. deletePreviousChar ( )

若没有选中文本,删除字符 before 当前光标位置;否则删除选中文本。

PySide2.QtGui.QTextCursor. document ( )
返回类型

QTextDocument

返回关联此光标的文档。

PySide2.QtGui.QTextCursor. endEditBlock ( )

指示应出现作为单操作的文档块编辑操作结束,从撤消/重做角度来看。

另请参阅

beginEditBlock()

PySide2.QtGui.QTextCursor. hasComplexSelection ( )
返回类型

bool

返回 true 若光标包含的选定范围不只是从 selectionStart() to selectionEnd() ;否则返回 false .

复杂选定是跨越表格至少 2 单元格的选择;其范围被指定通过 selectedTableCells() .

PySide2.QtGui.QTextCursor. hasSelection ( )
返回类型

bool

返回 true 光标包含选定;否则返回 false .

PySide2.QtGui.QTextCursor. insertBlock ( format , charFormat )
参数

这是重载函数。

插入新的空块在光标 position() with block format format and charFormat 作为块字符格式。

另请参阅

setBlockFormat()

PySide2.QtGui.QTextCursor. insertBlock ( format )
参数

format QTextBlockFormat

这是重载函数。

插入新的空块在光标 position() with block format format 和当前 charFormat() 作为块字符格式。

另请参阅

setBlockFormat()

PySide2.QtGui.QTextCursor. insertBlock ( )

插入新的空块在光标 position() with the current blockFormat() and charFormat() .

另请参阅

setBlockFormat()

PySide2.QtGui.QTextCursor. insertFragment ( fragment )
参数

fragment QTextDocumentFragment

插入文本 fragment 在当前 position() .

PySide2.QtGui.QTextCursor. insertFrame ( format )
参数

format QTextFrameFormat

返回类型

QTextFrame

插入框架采用给定 format 在当前光标 position() , moves the cursor position() inside the frame, and returns the frame.

若光标保有选定,整个选定被移入框架。

另请参阅

hasSelection()

PySide2.QtGui.QTextCursor. insertHtml ( html )
参数

html – unicode

插入文本 html 在当前 position() . The text is interpreted as HTML.

注意

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 setDefaultStyleSheet() 代替。

PySide2.QtGui.QTextCursor. insertImage ( image [ , name="" ] )
参数
PySide2.QtGui.QTextCursor. insertImage ( name )
参数

name – unicode

PySide2.QtGui.QTextCursor. insertImage ( format )
参数

format QTextImageFormat

PySide2.QtGui.QTextCursor. insertImage ( format , alignment )
参数
PySide2.QtGui.QTextCursor. insertList ( format )
参数

format QTextListFormat

返回类型

QTextList

PySide2.QtGui.QTextCursor. insertList ( style )
参数

style Style

返回类型

QTextList

这是重载函数。

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.

PySide2.QtGui.QTextCursor. insertTable ( rows , cols )
参数
  • rows int

  • cols int

返回类型

QTextTable

这是重载函数。

创建新表采用给定数量的 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.

另请参阅

currentTable()

PySide2.QtGui.QTextCursor. insertTable ( rows , cols , format )
参数
返回类型

QTextTable

创建新表采用给定数量的 rows and columns 以指定 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.

另请参阅

currentTable()

PySide2.QtGui.QTextCursor. insertText ( text )
参数

text – unicode

插入 text 在当前位置,使用当前字符格式。

若存在选定,选定被删除并替换通过 text ,例如:

cursor.clearSelection()
cursor.movePosition(QTextCursor.NextWord, QTextCursor.KeepAnchor)
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() 调用。

PySide2.QtGui.QTextCursor. insertText ( text , format )
参数

这是重载函数。

插入 text 在当前位置采用给定 format .

PySide2.QtGui.QTextCursor. isCopyOf ( other )
参数

other QTextCursor

返回类型

bool

返回 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.

另请参阅

operator=() operator==()

PySide2.QtGui.QTextCursor. isNull ( )
返回类型

bool

返回 true 若光标为 null;否则返回 false 。null 光标被创建通过默认构造函数。

PySide2.QtGui.QTextCursor. joinPreviousEditBlock ( )

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.

例如:

cursor = QTextCursor(textDocument)
cursor.beginEditBlock()
cursor.insertText("Hello")
cursor.insertText("World")
cursor.endEditBlock()
...
cursor.joinPreviousEditBlock()
cursor.insertText("Hey")
cursor.endEditBlock()
textDocument.undo()
											

调用 undo() 将导致所有 3 插入被撤消。

PySide2.QtGui.QTextCursor. keepPositionOnInsert ( )
返回类型

bool

返回是否应该保持光标在当前位置,当将文本插入在光标位置处时。

默认为 false;

PySide2.QtGui.QTextCursor. mergeBlockCharFormat ( modifier )
参数

modifier QTextCharFormat

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 .

PySide2.QtGui.QTextCursor. mergeBlockFormat ( modifier )
参数

modifier QTextBlockFormat

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

PySide2.QtGui.QTextCursor. mergeCharFormat ( modifier )
参数

modifier QTextCharFormat

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.

PySide2.QtGui.QTextCursor. movePosition ( op [ , arg__2=MoveAnchor [ , n=1 ] ] )
参数
返回类型

bool

移动光标通过履行给定 operation n 次,使用指定 mode ,并返回 true 若所有操作均成功完成;否则返回 false .

例如:若重复使用此函数以寻址下一单词的末尾,它最终将失败,当到达文档末尾时。

默认情况下,移动操作被履行一次 ( n = 1).

mode is KeepAnchor ,光标选择由它移动覆盖的文本。这与用户达成的效果相同,当按下 Shift 键和采用光标键移动光标时。

PySide2.QtGui.QTextCursor. __ne__ ( rhs )
参数

rhs QTextCursor

返回类型

bool

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

PySide2.QtGui.QTextCursor. __lt__ ( rhs )
参数

rhs QTextCursor

返回类型

bool

PySide2.QtGui.QTextCursor. __le__ ( rhs )
参数

rhs QTextCursor

返回类型

bool

PySide2.QtGui.QTextCursor. __eq__ ( rhs )
参数

rhs QTextCursor

返回类型

bool

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

PySide2.QtGui.QTextCursor. __gt__ ( rhs )
参数

rhs QTextCursor

返回类型

bool

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

PySide2.QtGui.QTextCursor. __ge__ ( rhs )
参数

rhs QTextCursor

返回类型

bool

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

PySide2.QtGui.QTextCursor. position ( )
返回类型

int

返回光标在文档中的绝对位置。光标位于字符之间。

注意

The “characters” in this case refer to the string of QChar 对象 (即:16 位 Unicode 字符),且位置被视为是此字符串的索引。这不必对应书写系统中的各个字形,因为单个字形可能由多个 Unicode 字符 (如代理对、语言连字或音调符号) 表示。

PySide2.QtGui.QTextCursor. positionInBlock ( )
返回类型

int

返回块内光标的相对位置。光标位于字符之间。

这相当于 position() - block().position() .

注意

The “characters” in this case refer to the string of QChar 对象 (即:16 位 Unicode 字符),且位置被视为是此字符串的索引。这不必对应书写系统中的各个字形,因为单个字形可能由多个 Unicode 字符 (如代理对、语言连字或音调符号) 表示。

另请参阅

position()

PySide2.QtGui.QTextCursor. removeSelectedText ( )

若存在选定,其内容被删除;否则什么都不做。

另请参阅

hasSelection()

PySide2.QtGui.QTextCursor. select ( selection )
参数

selection SelectionType

选择文档文本,根据给定 selection .

PySide2.QtGui.QTextCursor. selectedTableCells ( )

若选择跨越表格单元格, firstRow 是第一选定行的填充编号, firstColumn 是第一选定列的编号,而 numRows and numColumns 是选定行数和列数。若选定未跨越任何表格单元格,则结果是无害的但不确定。

PySide2.QtGui.QTextCursor. selectedText ( )
返回类型

unicode

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() 代替。

注意

若从编辑器获得的选定跨越换行符,文本将包含 Unicode U+2029 段落分隔符,而不是换行 \n 字符。使用 replace() to replace these characters with newlines.

PySide2.QtGui.QTextCursor. selection ( )
返回类型

QTextDocumentFragment

返回当前选定 (可能为空) 自带其所有格式信息。若仅仅希望选择文本 (即:纯文本),使用 selectedText() 代替。

注意

不像 toPlainText() , selectedText() may include special unicode characters such as ParagraphSeparator .

另请参阅

toPlainText()

PySide2.QtGui.QTextCursor. selectionEnd ( )
返回类型

int

返回选定的结束,或 position() if the cursor doesn’t have a selection.

PySide2.QtGui.QTextCursor. selectionStart ( )
返回类型

int

返回选定的起始,或 position() if the cursor doesn’t have a selection.

PySide2.QtGui.QTextCursor. setBlockCharFormat ( format )
参数

format QTextCharFormat

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

另请参阅

blockCharFormat()

PySide2.QtGui.QTextCursor. setBlockFormat ( format )
参数

format QTextBlockFormat

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

PySide2.QtGui.QTextCursor. setCharFormat ( format )
参数

format QTextCharFormat

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.

PySide2.QtGui.QTextCursor. setKeepPositionOnInsert ( b )
参数

b bool

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

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.

默认为 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.

PySide2.QtGui.QTextCursor. setPosition ( pos [ , mode=MoveAnchor ] )
参数

移动光标到文档绝对位置,指定通过 pos 使用 MoveMode 指定通过 m . The cursor is positioned between characters.

注意

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.

PySide2.QtGui.QTextCursor. setVerticalMovementX ( x )
参数

x int

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.

PySide2.QtGui.QTextCursor. setVisualNavigation ( b )
参数

b bool

把视觉导航设为 b .

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

PySide2.QtGui.QTextCursor. swap ( other )
参数

other QTextCursor

Swaps this text cursor instance with other 。此函数非常快,且从不失败。

PySide2.QtGui.QTextCursor. verticalMovementX ( )
返回类型

int

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.

PySide2.QtGui.QTextCursor. visualNavigation ( )
返回类型

bool

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

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