QTextCursor Class Reference

[ QtGui module]

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

类型

方法

Special Methods


详细描述

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 ().

QTextCursor 还有 anchor () position. The text that is between the anchor () 和 position () is the selection. If anchor () == position () there is no selection.

光标位置可以通过编程方式被改变,使用 setPosition () 和 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 () 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 ().

格式化可以被应用到当前文本文档,使用 setCharFormat (), mergeCharFormat (), setBlockFormat () 和 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 parts 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:

Actions can be grouped (i.e. treated as a single action for undo/redo) using beginEditBlock () 和 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 () 和 deleteChar () limit cursor movement to these valid positions.


类型文档编制

QTextCursor.MoveMode

常量 描述
QTextCursor.MoveAnchor 0 Moves the anchor to the same position as the cursor itself.
QTextCursor.KeepAnchor 1 保持锚点位置。

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

QTextCursor.MoveOperation

常量 描述
QTextCursor.NoMove 0 保持光标位置
QTextCursor.Start 1 移至文档开头。
QTextCursor.StartOfLine 3 移至当前行开头。
QTextCursor.StartOfBlock 4 移至当前块开头。
QTextCursor.StartOfWord 5 移至当前单词开头。
QTextCursor.PreviousBlock 6 移至先前块开头。
QTextCursor.PreviousCharacter 7 移至上一字符。
QTextCursor.PreviousWord 8 Move to the beginning of the previous word.
QTextCursor.Up 2 上移一行。
QTextCursor.Left 9 左移一字符。
QTextCursor.WordLeft 10 左移一单词。
QTextCursor.End 11 移至文档末尾。
QTextCursor.EndOfLine 13 移至当前行末尾。
QTextCursor.EndOfWord 14 移至当前单词末尾。
QTextCursor.EndOfBlock 15 移至当前块末尾。
QTextCursor.NextBlock 16 移至下一块起始。
QTextCursor.NextCharacter 17 移至下一字符。
QTextCursor.NextWord 18 移至下一单词。
QTextCursor.Down 12 下移一行。
QTextCursor.Right 19 右移一字符。
QTextCursor.WordRight 20 右移一单词。
QTextCursor.NextCell 21 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.
QTextCursor.PreviousCell 22 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.
QTextCursor.NextRow 23 Move to the first new cell of the next row in the current table.
QTextCursor.PreviousRow 24 Move to the last cell of the previous row in the current table.

另请参阅 movePosition ().

QTextCursor.SelectionType

This enum describes the types of selection that can be applied 采用 select () 函数。

常量 描述
QTextCursor.Document 3 选择整个文档。
QTextCursor.BlockUnderCursor 2 Selects the block of text under the 光标。
QTextCursor.LineUnderCursor 1 Selects the line of text under the 光标。
QTextCursor.WordUnderCursor 0 Selects the word under the cursor. If the cursor is not positioned within a string of selectable characters, no text is selected.

方法文档编制

QTextCursor.__init__ ( self )

构造 null 光标。

QTextCursor.__init__ ( self , QTextDocument   document )

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

QTextCursor.__init__ ( self , QTextFrame   frame )

QTextCursor.__init__ ( self , QTextBlock   block )

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

QTextCursor.__init__ ( self , QTextCursor   cursor )

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

int QTextCursor.anchor ( self )

返回锚点位置;这如同 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.

另请参阅 position (), setPosition (), movePosition (), selectionStart (),和 selectionEnd ().

bool QTextCursor.atBlockEnd ( self )

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

另请参阅 atBlockStart () 和 atEnd ().

bool QTextCursor.atBlockStart ( self )

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

另请参阅 atBlockEnd () 和 atStart ().

bool QTextCursor.atEnd ( self )

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

该函数在 Qt 4.6 引入。

另请参阅 atStart () and atBlockEnd ().

bool QTextCursor.atStart ( self )

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

另请参阅 atBlockStart () 和 atEnd ().

QTextCursor.beginEditBlock ( self )

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.

例如:

 QTextCursor cursor(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 beginEditBlock and endEditBlock. The top-most pair will determine the scope of the undo/redo operation.

另请参阅 endEditBlock ().

QTextBlock QTextCursor.block ( self )

返回包含光标的块。

QTextCharFormat QTextCursor.blockCharFormat ( self )

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.

另请参阅 setBlockCharFormat ().

QTextBlockFormat QTextCursor.blockFormat ( self )

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

另请参阅 setBlockFormat () 和 charFormat ().

int QTextCursor.blockNumber ( self )

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.

该函数在 Qt 4.2 引入。

QTextCharFormat QTextCursor.charFormat ( self )

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 被返回。

另请参阅 setCharFormat (), insertText (),和 blockFormat ().

QTextCursor.clearSelection ( self )

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

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

另请参阅 removeSelectedText () 和 hasSelection ().

int QTextCursor.columnNumber ( self )

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

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

该函数在 Qt 4.2 引入。

另请参阅 positionInBlock ().

QTextList QTextCursor.createList ( self , QTextListFormat   format )

创建并返回新列表采用给定 format ,和 makes the current paragraph the cursor is in the first list 项。

另请参阅 insertList () 和 currentList ().

QTextList QTextCursor.createList ( self , QTextListFormat.Style   style )

这是重载函数。

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

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

另请参阅 insertList () 和 currentList ().

QTextFrame QTextCursor.currentFrame ( self )

Returns a pointer to the current frame. Returns 0 if the cursor is invalid.

另请参阅 insertFrame ().

QTextList QTextCursor.currentList ( self )

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

另请参阅 insertList () 和 createList ().

QTextTable QTextCursor.currentTable ( self )

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

另请参阅 insertTable ().

QTextCursor.deleteChar ( self )

若没有选中文本,删除字符 at the current cursor position; otherwise deletes the selected text.

另请参阅 deletePreviousChar (), hasSelection (),和 clearSelection ().

QTextCursor.deletePreviousChar ( self )

若没有选中文本,删除字符 before the current cursor position; otherwise deletes the selected text.

另请参阅 deleteChar (), hasSelection (),和 clearSelection ().

QTextDocument QTextCursor.document ( self )

返回关联此光标的文档。

该函数在 Qt 4.5 引入。

QTextCursor.endEditBlock ( self )

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.

另请参阅 beginEditBlock ().

bool QTextCursor.hasComplexSelection ( self )

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

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

bool QTextCursor.hasSelection ( self )

Returns true if the cursor contains a selection; otherwise returns false.

QTextCursor.insertBlock ( self )

插入新的空块在光标 position () 采用当前 blockFormat () 和 charFormat ().

另请参阅 setBlockFormat ().

QTextCursor.insertBlock ( self , QTextBlockFormat   format )

这是重载函数。

插入新的空块在光标 position () 采用块格式 format 和当前 charFormat () as block char format.

另请参阅 setBlockFormat ().

QTextCursor.insertBlock ( self , QTextBlockFormat   format , QTextCharFormat   charFormat )

这是重载函数。

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

另请参阅 setBlockFormat ().

QTextCursor.insertFragment ( self , QTextDocumentFragment   fragment )

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

QTextFrame QTextCursor.insertFrame ( self , QTextFrameFormat   format )

插入框架采用给定 format 在当前 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.

另请参阅 hasSelection ().

QTextCursor.insertHtml ( self , QString  html )

插入文本 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 QTextDocument.setDefaultStyleSheet () 代替。

该函数在 Qt 4.2 引入。

QTextCursor.insertImage ( self , QTextImageFormat   format )

插入图像定义通过 format 在当前 position ().

QTextCursor.insertImage ( self , QTextImageFormat   format , QTextFrameFormat.Position   alignment )

这是重载函数。

插入图像定义通过给定 format 在 cursor's current position with the specified alignment .

该函数在 Qt 4.2 引入。

另请参阅 position ().

QTextCursor.insertImage ( self , QString  name )

这是重载函数。

用于插入图像的方便方法采用给定 name 在当前 position ().

 QImage img = ...
 textDocument->addResource(QTextDocument.ImageResource, QUrl("myimage"), img);
 cursor.insertImage("myimage");
			

QTextCursor.insertImage ( self , QImage   image , QString  name  = QString())

这是重载函数。

方便函数用于插入给定 image with an optional name 在当前 position ().

该函数在 Qt 4.5 引入。

QTextList QTextCursor.insertList ( self , 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.

另请参阅 currentList (), createList (),和 insertBlock ().

QTextList QTextCursor.insertList ( self , QTextListFormat.Style   style )

这是重载函数。

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.

另请参阅 currentList (), createList (),和 insertBlock ().

QTextTable QTextCursor.insertTable ( self , int  rows , int  cols , QTextTableFormat   format )

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

QTextTable QTextCursor.insertTable ( self , int  rows , int  cols )

这是重载函数。

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

QTextCursor.insertText ( self , QString  text )

插入 text at the current position, using the current character format.

If there is a selection, the selection is deleted and replaced by 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 () calls.

另请参阅 charFormat () 和 hasSelection ().

QTextCursor.insertText ( self , QString  text , QTextCharFormat   format )

这是重载函数。

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

bool QTextCursor.isCopyOf ( self , QTextCursor   other )

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.

另请参阅 operator= () 和 operator== ().

bool QTextCursor.isNull ( self )

Returns true if the cursor is null; otherwise returns false. A null cursor is created by the default constructor.

QTextCursor.joinPreviousEditBlock ( self )

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.

例如:

 QTextCursor cursor(textDocument);
 cursor.beginEditBlock();
 cursor.insertText("Hello");
 cursor.insertText("World");
 cursor.endEditBlock();
 ...
 cursor.joinPreviousEditBlock();
 cursor.insertText("Hey");
 cursor.endEditBlock();
 textDocument->undo();
			

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

另请参阅 beginEditBlock () 和 endEditBlock ().

bool QTextCursor.keepPositionOnInsert ( self )

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

默认为 false;

该函数在 Qt 4.7 引入。

另请参阅 setKeepPositionOnInsert ().

QTextCursor.mergeBlockCharFormat ( self , QTextCharFormat   modifier )

Modifies the block char format of the current block (or all blocks that are contained in the selection) with the block format 指定通过 modifier .

另请参阅 setBlockCharFormat ().

QTextCursor.mergeBlockFormat ( self , QTextBlockFormat   modifier )

Modifies the block format of the current block (or all blocks that are contained in the selection) with the block format 指定通过 modifier .

另请参阅 setBlockFormat () 和 blockFormat ().

QTextCursor.mergeCharFormat ( self , 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.

另请参阅 hasSelection () 和 setCharFormat ().

bool QTextCursor.movePosition ( self , MoveOperation   op , MoveMode   mode  = QTextCursor.MoveAnchor, int  n  = 1)

移动光标通过履行给定 operation n 次,使用指定 mode , and returns true 若所有操作均成功完成;否则返回 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.

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

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.

另请参阅 setVisualNavigation ().

int QTextCursor.position ( self )

Returns the absolute position of the cursor within the document. The cursor is positioned between characters.

另请参阅 setPosition (), movePosition (), anchor (),和 positionInBlock ().

int QTextCursor.positionInBlock ( self )

Returns the relative position of the cursor within the block. The cursor is positioned between characters.

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

该函数在 Qt 4.7 引入。

另请参阅 position ().

QTextCursor.removeSelectedText ( self )

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

另请参阅 hasSelection ().

QTextCursor.select ( self , SelectionType   selection )

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

(int  firstRow , int  numRows , int  firstColumn , int  numColumns ) QTextCursor.selectedTableCells ( self )

若选择跨越表格单元格, 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.

QString QTextCursor.selectedText ( self )

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

注意: 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 \n 字符。使用 QString.replace () to replace these characters with newlines.

QTextDocumentFragment QTextCursor.selection ( self )

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

注意: 不像 QTextDocumentFragment.toPlainText (), selectedText () may include special unicode characters such as QChar.ParagraphSeparator .

另请参阅 QTextDocumentFragment.toPlainText ().

int QTextCursor.selectionEnd ( self )

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

另请参阅 selectionStart (), position (),和 anchor ().

int QTextCursor.selectionStart ( self )

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

另请参阅 selectionEnd (), position (),和 anchor ().

QTextCursor.setBlockCharFormat ( self , QTextCharFormat   format )

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

另请参阅 blockCharFormat ().

QTextCursor.setBlockFormat ( self , QTextBlockFormat   format )

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

另请参阅 blockFormat () 和 mergeBlockFormat ().

QTextCursor.setCharFormat ( self , 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.

另请参阅 charFormat (), hasSelection (),和 mergeCharFormat ().

QTextCursor.setKeepPositionOnInsert ( self , bool  b )

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.

该函数在 Qt 4.7 引入。

另请参阅 keepPositionOnInsert ().

QTextCursor.setPosition ( self , int  pos , MoveMode   mode  = QTextCursor.MoveAnchor)

Moves the cursor to the absolute position in the document 指定通过 pos 使用 MoveMode 指定通过 m . The cursor is positioned between characters.

另请参阅 position (), movePosition (),和 anchor ().

QTextCursor.setVerticalMovementX ( self , int  x )

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.

该函数在 Qt 4.7 引入。

另请参阅 verticalMovementX ().

QTextCursor.setVisualNavigation ( self , bool  b )

把视觉导航设为 b .

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

该函数在 Qt 4.4 引入。

另请参阅 visualNavigation () 和 movePosition ().

int QTextCursor.verticalMovementX ( self )

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.

该函数在 Qt 4.7 引入。

另请参阅 setVerticalMovementX ().

bool QTextCursor.visualNavigation ( self )

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

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

该函数在 Qt 4.4 引入。

另请参阅 setVisualNavigation () and movePosition ().

bool QTextCursor.__eq__ ( self , QTextCursor   rhs )

bool QTextCursor.__ge__ ( self , QTextCursor   rhs )

bool QTextCursor.__gt__ ( self , QTextCursor   rhs )

bool QTextCursor.__le__ ( self , QTextCursor   rhs )

bool QTextCursor.__lt__ ( self , QTextCursor   rhs )

bool QTextCursor.__ne__ ( self , QTextCursor   rhs )