CopperSpice API
1.9.2
|
The QTextCursor class offers an API to access and modify QTextDocuments. More...
Public Types | |
enum | MoveMode |
enum | MoveOperation |
enum | SelectionType |
Public Methods | |
QTextCursor () | |
QTextCursor (const QTextBlock &block) | |
QTextCursor (const QTextCursor &other) | |
QTextCursor (QTextDocument *document) | |
QTextCursor (QTextFrame *frame) | |
~QTextCursor () | |
int | anchor () const |
bool | atBlockEnd () const |
bool | atBlockStart () const |
bool | atEnd () const |
bool | atStart () const |
void | beginEditBlock () |
QTextBlock | block () const |
QTextCharFormat | blockCharFormat () const |
QTextBlockFormat | blockFormat () const |
int | blockNumber () const |
QTextCharFormat | charFormat () const |
void | clearSelection () |
int | columnNumber () const |
QTextList * | createList (const QTextListFormat &format) |
QTextList * | createList (QTextListFormat::Style style) |
QTextFrame * | currentFrame () const |
QTextList * | currentList () const |
QTextTable * | currentTable () const |
void | deleteChar () |
void | deletePreviousChar () |
QTextDocument * | document () const |
void | endEditBlock () |
bool | hasComplexSelection () const |
bool | hasSelection () const |
void | insertBlock () |
void | insertBlock (const QTextBlockFormat &format) |
void | insertBlock (const QTextBlockFormat &format, const QTextCharFormat &charFormat) |
void | insertFragment (const QTextDocumentFragment &fragment) |
QTextFrame * | insertFrame (const QTextFrameFormat &format) |
void | insertHtml (const QString &html) |
void | insertImage (const QImage &image, const QString &name=QString ()) |
void | insertImage (const QString &name) |
void | insertImage (const QTextImageFormat &format) |
void | insertImage (const QTextImageFormat &format, QTextFrameFormat::Position alignment) |
QTextList * | insertList (const QTextListFormat &format) |
QTextList * | insertList (QTextListFormat::Style style) |
QTextTable * | insertTable (int rows, int cols) |
QTextTable * | insertTable (int rows, int cols, const QTextTableFormat &format) |
void | insertText (const QString &text) |
void | insertText (const QString &text, const QTextCharFormat &format) |
bool | isCopyOf (const QTextCursor &other) const |
bool | isNull () const |
void | joinPreviousEditBlock () |
bool | keepPositionOnInsert () const |
void | mergeBlockCharFormat (const QTextCharFormat &modifier) |
void | mergeBlockFormat (const QTextBlockFormat &modifier) |
void | mergeCharFormat (const QTextCharFormat &modifier) |
bool | movePosition (MoveOperation operation, MoveMode mode=MoveAnchor, int n=1) |
bool | operator!= (const QTextCursor &other) const |
bool | operator< (const QTextCursor &other) const |
bool | operator<= (const QTextCursor &other) const |
QTextCursor & | operator= (const QTextCursor &other) |
QTextCursor & | operator= (QTextCursor &&other) |
bool | operator== (const QTextCursor &other) const |
bool | operator> (const QTextCursor &other) const |
bool | operator>= (const QTextCursor &other) const |
int | position () const |
int | positionInBlock () const |
void | removeSelectedText () |
void | select (SelectionType selection) |
void | selectedTableCells (int *firstRow, int *numRows, int *firstColumn, int *numColumns) const |
QString | selectedText () const |
QTextDocumentFragment | selection () const |
int | selectionEnd () const |
int | selectionStart () const |
void | setBlockCharFormat (const QTextCharFormat &format) |
void | setBlockFormat (const QTextBlockFormat &format) |
void | setCharFormat (const QTextCharFormat &format) |
void | setKeepPositionOnInsert (bool b) |
void | setPosition (int pos, MoveMode mode=MoveAnchor) |
void | setVerticalMovementX (int x) |
void | setVisualNavigation (bool b) |
void | swap (QTextCursor &other) |
int | verticalMovementX () const |
bool | visualNavigation () const |
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 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().
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:
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.
Constant | Value | Description |
---|---|---|
QTextCursor::MoveAnchor | 0 | Moves the anchor to the same position as the cursor itself. |
QTextCursor::KeepAnchor | 1 | 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.
Constant | Value | Description |
---|---|---|
QTextCursor::NoMove | 0 | Keep the cursor where it is |
QTextCursor::Start | 1 | Move to the start of the document. |
QTextCursor::StartOfLine | 3 | Move to the start of the current line. |
QTextCursor::StartOfBlock | 4 | Move to the start of the current block. |
QTextCursor::StartOfWord | 5 | Move to the start of the current word. |
QTextCursor::PreviousBlock | 6 | Move to the start of the previous block. |
QTextCursor::PreviousCharacter | 7 | Move to the previous character. |
QTextCursor::PreviousWord | 8 | Move to the beginning of the previous word. |
QTextCursor::Up | 2 | Move up one line. |
QTextCursor::Left | 9 | Move left one character. |
QTextCursor::WordLeft | 10 | Move left one word. |
QTextCursor::End | 11 | Move to the end of the document. |
QTextCursor::EndOfLine | 13 | Move to the end of the current line. |
QTextCursor::EndOfWord | 14 | Move to the end of the current word. |
QTextCursor::EndOfBlock | 15 | Move to the end of the current block. |
QTextCursor::NextBlock | 16 | Move to the beginning of the next block. |
QTextCursor::NextCharacter | 17 | Move to the next character. |
QTextCursor::NextWord | 18 | Move to the next word. |
QTextCursor::Down | 12 | Move down one line. |
QTextCursor::Right | 19 | Move right one character. |
QTextCursor::WordRight | 20 | Move right one word. |
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. |
This enum describes the types of selection that can be applied with the select() function.
Constant | Value | Description |
---|---|---|
QTextCursor::Document | 3 | Selects the entire document. |
QTextCursor::BlockUnderCursor | 2 | Selects the block of text under the cursor. |
QTextCursor::LineUnderCursor | 1 | Selects the line of text under the cursor. |
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::QTextCursor | ( | ) |
Constructs a null cursor.
|
explicit |
Constructs a cursor pointing to the beginning of the document.
|
explicit |
Constructs a cursor pointing to the beginning of the frame.
|
explicit |
Constructs a cursor pointing to the beginning of the block.
QTextCursor::QTextCursor | ( | const QTextCursor & | other | ) |
Copy constructs a new QTextCursor from other.
QTextCursor::~QTextCursor | ( | ) |
Destroys the QTextCursor.
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.
bool QTextCursor::atBlockEnd | ( | ) | const |
Returns true if the cursor is at the end of a block, otherwise returns false.
bool QTextCursor::atBlockStart | ( | ) | const |
Returns true if the cursor is at the start of a block, otherwise returns false.
bool QTextCursor::atEnd | ( | ) | const |
Returns true if the cursor is at the end of the document, otherwise returns false.
bool QTextCursor::atStart | ( | ) | const |
Returns true if the cursor is at the start of the document, otherwise returns false.
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.
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.
QTextBlock QTextCursor::block | ( | ) | const |
Returns the block that contains the cursor.
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.
QTextBlockFormat QTextCursor::blockFormat | ( | ) | const |
Returns the block format of the block the cursor is in.
int QTextCursor::blockNumber | ( | ) | const |
Returns the number of the block the cursor is in or a nullptr if the cursor is invalid. This method only makes sense in documents without complex objects such as tables or frames.
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.
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.
int QTextCursor::columnNumber | ( | ) | const |
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.
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.
QTextList * QTextCursor::createList | ( | QTextListFormat::Style | style | ) |
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.
QTextFrame * QTextCursor::currentFrame | ( | ) | const |
Returns a pointer to the current frame. Returns a nullptr if the cursor is invalid.
QTextList * QTextCursor::currentList | ( | ) | const |
Returns the current list if the cursor position() is inside a block that is part of a list, otherwise returns a nullptr.
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 a nullptr.
void QTextCursor::deleteChar | ( | ) |
If there is no selected text, deletes the character at the current cursor position,, otherwise deletes the selected text.
void QTextCursor::deletePreviousChar | ( | ) |
If there is no selected text, deletes the character before the current cursor position, otherwise deletes the selected text.
QTextDocument * QTextCursor::document | ( | ) | const |
Returns the document this cursor is associated with.
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.
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().
bool QTextCursor::hasSelection | ( | ) | const |
Returns true if the cursor contains a selection, otherwise returns false.
void QTextCursor::insertBlock | ( | ) |
Inserts a new empty block at the cursor position() with the current blockFormat() and charFormat().
void QTextCursor::insertBlock | ( | const QTextBlockFormat & | format | ) |
Inserts a new empty block at the cursor position() with the given block format and the current charFormat().
void QTextCursor::insertBlock | ( | const QTextBlockFormat & | format, |
const QTextCharFormat & | charFormat | ||
) |
Inserts a new empty block at the cursor position() with the given block format and charFormat.
void QTextCursor::insertFragment | ( | const QTextDocumentFragment & | fragment | ) |
Inserts the text fragment at the current position().
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.
void QTextCursor::insertHtml | ( | const QString & | html | ) |
Inserts the text html at the current position(). The text is interpreted as HTML.
This method is used to insert the given image with an optional name at the current position().
void QTextCursor::insertImage | ( | const QString & | name | ) |
This method is used to insert the image with the given name at the current position().
void QTextCursor::insertImage | ( | const QTextImageFormat & | format | ) |
Inserts the image defined by the given format at the current position().
void QTextCursor::insertImage | ( | const QTextImageFormat & | format, |
QTextFrameFormat::Position | alignment | ||
) |
Inserts the image defined by the given format at the cursor's current position with the specified alignment.
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.
QTextList * QTextCursor::insertList | ( | 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.
QTextTable * QTextCursor::insertTable | ( | int | rows, |
int | cols | ||
) |
Creates a new table with the given number of rows and cols, 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.
QTextTable * QTextCursor::insertTable | ( | int | rows, |
int | cols, | ||
const QTextTableFormat & | format | ||
) |
Creates a new table with the given number of rows and cols 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.
void QTextCursor::insertText | ( | const QString & | text | ) |
Inserts text at the current position using the current character format. If there is a selection, it is deleted and replaced by the given text.
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.
void QTextCursor::insertText | ( | const QString & | text, |
const QTextCharFormat & | format | ||
) |
Inserts text at the current position with the given format.
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.
bool QTextCursor::isNull | ( | ) | const |
Returns true if the cursor is null, otherwise returns false. A null cursor is created by the default constructor.
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.
The call to undo() will cause all three insertions to be undone.
bool QTextCursor::keepPositionOnInsert | ( | ) | const |
Returns whether the cursor should keep its current position when text gets inserted at the position of the cursor.
The default is false;
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.
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.
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 method applies all the properties set in modifier to all the character formats that are part of the selection.
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 method 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.
bool QTextCursor::operator!= | ( | const QTextCursor & | other | ) | const |
Returns true if the other cursor is at a different position in the document as this cursor, otherwise returns false.
bool QTextCursor::operator< | ( | const QTextCursor & | other | ) | const |
Returns true if the other cursor is positioned later in the document than this cursor, otherwise returns false.
bool QTextCursor::operator<= | ( | const QTextCursor & | other | ) | const |
Returns true if the other cursor is positioned later or at the same position in the document as this cursor, otherwise returns false.
QTextCursor & QTextCursor::operator= | ( | const QTextCursor & | other | ) |
Copy assigns from other and returns a reference to this object.
|
inline |
Move assigns from other and returns a reference to this object.
bool QTextCursor::operator== | ( | const QTextCursor & | other | ) | const |
Returns true if the other cursor is at the same position in the document as this cursor, otherwise returns false.
bool QTextCursor::operator> | ( | const QTextCursor & | other | ) | const |
Returns true if the other cursor is positioned earlier in the document than this cursor, otherwise returns false.
bool QTextCursor::operator>= | ( | const QTextCursor & | other | ) | const |
Returns true if the other cursor is positioned earlier or at the same position in the document as this cursor, otherwise returns false.
int QTextCursor::position | ( | ) | const |
Returns the absolute position of the cursor within the document. The cursor is positioned between characters.
int QTextCursor::positionInBlock | ( | ) | const |
Returns the relative position of the cursor within the block. The cursor is positioned between characters.
This is equivalent to position() - block().position()
.
void QTextCursor::removeSelectedText | ( | ) |
If there is a selection, its content is deleted, otherwise does nothing.
void QTextCursor::select | ( | SelectionType | selection | ) |
Selects text in the document according to the given selection.
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.
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.
character. Use QString::replace() to replace these characters with newlines. 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.
int QTextCursor::selectionEnd | ( | ) | const |
Returns the end of the selection or position() if the cursor does not have a selection.
int QTextCursor::selectionStart | ( | ) | const |
Returns the start of the selection or position() if the cursor does not have a selection.
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.
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.
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.
void QTextCursor::setKeepPositionOnInsert | ( | bool | b | ) |
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.
void QTextCursor::setPosition | ( | int | pos, |
MoveMode | mode = MoveAnchor |
||
) |
Moves the cursor to the absolute position in the document specified by pos using a MoveMode
specified by mode. The cursor is positioned between characters.
void QTextCursor::setVerticalMovementX | ( | 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.
void QTextCursor::setVisualNavigation | ( | bool | b | ) |
Sets visual navigation to b.
Visual navigation means skipping over hidden text paragraphs. The default is false.
|
inline |
Swaps this text cursor with other. This method is very fast and never fails.
int QTextCursor::verticalMovementX | ( | ) | const |
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.
bool QTextCursor::visualNavigation | ( | ) | const |
Returns true if the cursor does visual navigation, otherwise returns false.
Visual navigation means skipping over hidden text paragraphs. The default is false.