Logo Search packages:      
Sourcecode: qt4-x11 version File versions  Download package

QTextCursor Class Reference

List of all members.

Detailed Description

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 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, 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 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 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:

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

See also:
Rich Text Processing

Definition at line 70 of file qtextcursor.h.

Public Types

enum  MoveMode { MoveAnchor, KeepAnchor }
enum  MoveOperation {
  NoMove, Start, Up, StartOfLine,
  StartOfBlock, StartOfWord, PreviousBlock, PreviousCharacter,
  PreviousWord, Left, WordLeft, End,
  Down, EndOfLine, EndOfWord, EndOfBlock,
  NextBlock, NextCharacter, NextWord, Right,
enum  SelectionType { WordUnderCursor, LineUnderCursor, BlockUnderCursor, Document }

Public Member Functions

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
QTextListcreateList (QTextListFormat::Style style)
QTextListcreateList (const QTextListFormat &format)
QTextFramecurrentFrame () const
QTextListcurrentList () const
QTextTablecurrentTable () const
void deleteChar ()
void deletePreviousChar ()
void endEditBlock ()
bool hasComplexSelection () const
bool hasSelection () const
void insertBlock (const QTextBlockFormat &format, const QTextCharFormat &charFormat)
void insertBlock (const QTextBlockFormat &format)
void insertBlock ()
void insertFragment (const QTextDocumentFragment &fragment)
QTextFrameinsertFrame (const QTextFrameFormat &format)
void insertHtml (const QString &html)
void insertImage (const QString &name)
void insertImage (const QTextImageFormat &format)
void insertImage (const QTextImageFormat &format, QTextFrameFormat::Position alignment)
QTextListinsertList (QTextListFormat::Style style)
QTextListinsertList (const QTextListFormat &format)
QTextTableinsertTable (int rows, int cols)
QTextTableinsertTable (int rows, int cols, const QTextTableFormat &format)
void insertText (const QString &text, const QTextCharFormat &format)
void insertText (const QString &text)
bool isCopyOf (const QTextCursor &other) const
bool isNull () const
void joinPreviousEditBlock ()
void mergeBlockCharFormat (const QTextCharFormat &modifier)
void mergeBlockFormat (const QTextBlockFormat &modifier)
void mergeCharFormat (const QTextCharFormat &modifier)
bool movePosition (MoveOperation op, MoveMode=MoveAnchor, int n=1)
bool operator!= (const QTextCursor &rhs) const
bool operator< (const QTextCursor &rhs) const
bool operator<= (const QTextCursor &rhs) const
QTextCursoroperator= (const QTextCursor &other)
bool operator== (const QTextCursor &rhs) const
bool operator> (const QTextCursor &rhs) const
bool operator>= (const QTextCursor &rhs) const
int position () const
 QTextCursor (const QTextCursor &cursor)
 QTextCursor (QTextCursorPrivate *d)
 QTextCursor (const QTextBlock &block)
 QTextCursor (QTextFrame *frame)
 QTextCursor (QTextDocumentPrivate *p, int pos)
 QTextCursor (QTextDocument *document)
 QTextCursor ()
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 setPosition (int pos, MoveMode mode=MoveAnchor)
 ~QTextCursor ()

Private Attributes

< QTextCursorPrivate > 


class QTextCopyHelper
class QTextDocumentFragmentPrivate

The documentation for this class was generated from the following files:

Generated by  Doxygen 1.6.0   Back to index