Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions

QTextCursor Class Reference

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

#include <QTextCursor>

List of all members.

Public Types

Public Functions


Detailed Description

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

A QTextCursor is an object that can be used to access and manipulate a QTextDocument. It embodies both a cursor position and optionally a selection.

QTextCursor is modelled on how a text cursor behaves in a text editor, providing a programmatic means of doing what users do through the user interface. A document can be thought of as a single string of characters with the cursor's position() being between any two characters (or at the very beginning or very end of the document). Documents can also contain tables, lists, images, etc., in addition to text, but from the APIs point of view the document is just one long string, with some portions of that string considered to be within particular blocks (e.g. paragraphs), or within a table's cell, or a list's item, etc. When we refer to "current character" we mean the character immediately after 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 atEnd() 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 character (the character immedately after position()) using applyCharFormatModifier(), and to the current block (the block that contains position()) using setBlockFormat() and applyBlockFormatModifier(). The text at the current character position can be turned into a list using createList().

Deletions can be achieved using deleteChar(), deletePreviousChar(), and removeSelectedText(). Insertions are done using insertText(), insertBlock(), insertList(), insertTable(), insertImage(), insertFrame(), and insertFragment().

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 usually after every character in the text. 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.


Member Type Documentation

enum QTextCursor::MoveMode

QTextCursor::MoveAnchorMoves the anchor to the same position as the cursor itself.
QTextCursor::KeepAnchorKeeps 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.

enum QTextCursor::MoveOperation

QTextCursor::NoMoveKeep the cursor where it is
QTextCursor::StartMove to the start of the document
QTextCursor::StartOfLineMove to the start of the current line
QTextCursor::StartOfBlockMove to the start of the current block
QTextCursor::StartOfWordMove to the start of the current word
QTextCursor::PreviousBlockmove to the start of the previous block
QTextCursor::PreviousCharactermove to the previous character
QTextCursor::PreviousWordmove to the beginning of the previous word
QTextCursor::Upmove up one line
QTextCursor::Leftmove left one character
QTextCursor::WordLeftmove left one word
QTextCursor::Endmove to the end of the document
QTextCursor::EndOfLinemove to the end of the current line
QTextCursor::EndOfWordmove to the end of the current word
QTextCursor::EndOfBlockmove to the end of the current block
QTextCursor::NextBlockmove to the beginning of the next block
QTextCursor::NextCharactermove to the next character
QTextCursor::NextWordmove to the next word
QTextCursor::Downmove down one line
QTextCursor::Rightmove right one character
QTextCursor::WordRightright move one word

See also movePosition().


Member Function Documentation

QTextCursor::QTextCursor ()

Constructs a null cursor.

QTextCursor::QTextCursor ( QTextDocument * document )

Constructs a cursor pointing to the beginning of the document.

QTextCursor::QTextCursor ( QTextFrame * frame )

Constructs a cursor pointing to the beginning of the frame.

QTextCursor::QTextCursor ( const QTextBlock & block )

Constructs a cursor pointing to the beginning of the block.

QTextCursor::QTextCursor ( const QTextCursor & cursor )

Constructs a new cursor that is a copy of cursor.

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.

See also position(), setPosition(), movePosition(), selectionStart(), and selectionEnd().

bool QTextCursor::atBlockStart () const

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

See also atEnd().

bool QTextCursor::atEnd () const

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

See also atBlockStart().

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.

For example:

    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.

See also endEditBlock().

QTextBlock QTextCursor::block () const

Returns an iterator for the block that contains the cursor.

QTextBlockFormat QTextCursor::blockFormat () const

Returns the block format of the block the cursor is in.

See also setBlockFormat() and charFormat().

QTextCharFormat QTextCursor::charFormat () const

Returns the format of the character immediately before the cursor position().

See also insertText() and blockFormat().

void QTextCursor::clearSelection ()

Clears the current selection.

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

See also removeSelectedText() and hasSelection().

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.

See also insertList() and currentList().

QTextList * QTextCursor::createList ( int style )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Creates and returns a new list with the given style and makes the current paragraph the cursor is in the first list item.

See also insertList() and currentList().

QTextFrame * QTextCursor::currentFrame () const

Returns a pointer to the current frame, returns a null pointer if the cursor is invalid.

See also insertFrame().

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 null pointer.

See also insertList() and createList().

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 null pointer.

See also insertTable().

void QTextCursor::deleteChar ()

If there is no selected text, deletes the character at the current cursor position; otherwise deletes the selected text.

See also deletePreviousChar(), hasSelection(), and clearSelection().

void QTextCursor::deletePreviousChar ()

If there is no selected text, deletes the character before the current cursor position; otherwise deletes the selected text.

See also deleteChar(), hasSelection(), and clearSelection().

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.

See also beginEditBlock().

bool QTextCursor::hasComplexSelection () const

Returns true if the cursor contains a selection and the selection is simple a range from selectionStart() to selectionEnd(); otherwise returns false.

Complex selections are ones that span at least two rows of 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().

See also setBlockFormat().

void QTextCursor::insertBlock ( const QTextBlockFormat & format )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Inserts a new empty block at the cursor position() with block format format and the current charFormat().

See also setBlockFormat().

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(), and moves the cursor position() inside the frame.

If the cursor holds a selection the whole selection is moved inside the frame.

See also hasSelection().

void QTextCursor::insertImage ( const QTextImageFormat & format )

Inserts the image defined by format at the current position().

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.

See also currentList(), createList(), and insertBlock().

QTextList * QTextCursor::insertList ( int style )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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.

See also currentList(), createList(), and insertBlock().

QTextTable * QTextCursor::insertTable ( int rows, int cols, const QTextTableFormat & format )

Creates a new table with rows rows and cols columns, using the given format, inserts it at the current position(), and returns the table object. The cursor position() is moved to the beginning of the first cell.

See also currentTable().

QTextTable * QTextCursor::insertTable ( int rows, int cols )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Creates a new table with rows rows and cols columns, inserts it at the current position(), and returns the table object. The cursor position() is moved to the beginning of the first cell.

See also currentTable().

void QTextCursor::insertText ( const QString & text )

Inserts text at the current position, using the current character format.

If there is a selection, the selection is deleted and replaced by text, for example:

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

See also charFormat() and hasSelection().

void QTextCursor::insertText ( const QString & text, const QTextCharFormat & format )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Inserts text at the current position with the given format.

bool QTextCursor::isNull () const

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

void QTextCursor::mergeBlockFormat ( const QTextBlockFormat & modifier )

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

See also setBlockFormat().

void QTextCursor::mergeCharFormat ( const QTextCharFormat & modifier )

Applies all the properties set in modifier to all the formats that are part of the selection. Does nothing if the cursor doesn't have a selection.

See also hasSelection().

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

Moves the cursor in accordance with the MoveOperation op, using MoveMode mode. The move is performed n (default 1) times.

If mode is KeepAnchor, the cursor selects the text it moves over; (this is the same effect that the user achieves when they move using arrow keys etc., with the Shift key pressed).

int QTextCursor::position () const

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

See also setPosition(), movePosition(), and anchor().

void QTextCursor::removeSelectedText ()

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

See also hasSelection().

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 formatting information. If you want a document fragment (i.e. formatted text) use selection() instead.

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 doesn't have a selection.

See also selectionStart(), position(), and anchor().

int QTextCursor::selectionStart () const

Returns the start of the selection or position() if the cursor doesn't have a selection.

See also selectionEnd(), position(), and anchor().

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.

See also blockFormat().

void QTextCursor::setCharFormat ( const QTextCharFormat & format )

Set the format format as char format for the selection. Does nothing if the cursor doesn't have a selection.

See also hasSelection().

void QTextCursor::setPosition ( int pos, MoveMode m = MoveAnchor )

Moves the cursor to the absolute position pos using MoveMode m. The cursor is positioned between characters.

See also position(), movePosition(), and anchor().

bool QTextCursor::operator!= ( const QTextCursor & rhs ) const

Returns true if the rhs cursor is at a different position in the document as this cursor; otherwise returns false.

bool QTextCursor::operator< ( const QTextCursor & rhs ) const

Returns true if the rhs cursor is positioned later in the document than this cursor; otherwise returns false.

bool QTextCursor::operator<= ( const QTextCursor & rhs ) const

Returns true if the rhs cursor is positioned later or at the same position in the document as this cursor; otherwise returns false.

QTextCursor & QTextCursor::operator= ( const QTextCursor & cursor )

Makes a copy of cursor and assigns it to this QTextCursor.

bool QTextCursor::operator== ( const QTextCursor & rhs ) const

Returns true if the rhs cursor is at the same position in the document as this cursor; otherwise returns false.

bool QTextCursor::operator> ( const QTextCursor & rhs ) const

Returns true if the rhs cursor is positioned earlier in the document than this cursor; otherwise returns false.

bool QTextCursor::operator>= ( const QTextCursor & rhs ) const

Returns true if the rhs cursor is positioned earlier or at the same position in the document as this cursor; otherwise returns false.


Copyright © 2004 Trolltech. Trademarks
Qt 4.0.0-tp1