Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |
The QTextEdit widget provides a sophisticated single-page rich text editor. More...
#include <qtextedit.h>
Inherits QScrollView.
Inherited by QMultiLineEdit, QTextBrowser and QTextView.
QTextEdit is an advanced WYSIWYG editor supporting rich text formatting. It is optimized to handle large documents and to respond quickly to user input.
QTextEdit works on paragraphs and characters. A paragraph is a formatted string which is word-wrapped to fit into the width of the widget. A document consists of zero or more paragraphs, indexed from 0. Characters are indexed on a per-paragraph basis, also indexed from 0. The words in the paragraph are aligned in accordance with the paragraph's alignment(). Paragraphs are separated by hard line breaks. Each character within a paragraph has its own attributes, for example, font and color.
QTextEdit can display images (using QMimeSourceFactory), lists and tables. If the text is too large to view within the text edit's viewport, scrollbars will appear. The text edit can load both plain text and HTML files (a subset of HTML 3.2 and 4). The rendering style and the set of valid tags are defined by a styleSheet(). Change the style sheet with setStyleSheet(); see QStyleSheet for details. The images identified by image tags are displayed if they can be interpreted using the text edit's QMimeSourceFactory; see setMimeSourceFactory().
If you want a text browser with more navigation use QTextBrowser. If you just need to display a small piece of rich text use QLabel or QSimpleRichText.
If you create a new QTextEdit, and want to allow the user to edit rich text, call setTextFormat(Qt::RichText) to ensure that the text is treated as rich text. (Rich text uses HTML tags to set text formatting attributes. See QStyleSheet for information on the HTML tags that are supported.). If you don't call setTextFormat() explicitly the text edit will guess from the text itself whether it is rich text or plain text. This means that if the text looks like HTML or XML it will probably be interpreted as rich text, so you should call setTextFormat(Qt::PlainText) to preserve such text.
The text edit documentation uses the following concepts:
The text is set or replaced using setText() which deletes any existing text and replaces it with the text passed in the setText() call. Text can be inserted with insert(), paste() and pasteSubType(). Text can also be cut(). The entire text is deleted with clear() and the selected text is deleted with removeSelectedText(). Selected (marked) text can also be deleted with del() (which will delete the character to the right of the cursor if no text is selected).
The current format's attributes are set with setItalic(), setBold(), setUnderline(), setFamily() (font family), setPointSize(), setColor() and setCurrentFont(). The current paragraph's style is set with setParagType() and its alignment is set with setAlignment().
Use setSelection() to select text. The setSelectionAttributes() function is used to indicate how selected text should be displayed. Use hasSelectedText() to find out if any text is selected. The currently selected text's position is available using getSelection() and the selected text itself is returned by selectedText(). The selection can be copied to the clipboard with copy(), or cut to the clipboard with cut(). It can be deleted with removeSelectedText(). The entire text can be selected (or unselected) using selectAll(). QTextEdit supports multiple selections. Most of the selection functions operate on the default selection, selection 0. If the user presses a non-selecting key, e.g. a cursor key without also holding down Shift, all selections are cleared.
Set and get the position of the cursor with setCursorPosition() and getCursorPosition() respectively. When the cursor is moved, the signals currentFontChanged(), currentColorChanged() and currentAlignmentChanged() are emitted to reflect the font, color and alignment at the new cursor position.
If the text changes, the textChanged() signal is emitted, and if the user inserts a new line by pressing Return or Enter, returnPressed() is emitted. The isModified() function will return TRUE if the text has been modified.
QTextEdit provides command-based undo and redo. To set the depth of the command history use setUndoDepth() which defaults to 100 steps. To undo or redo the last operation call undo() or redo(). The signals undoAvailable() and redoAvailable() indicate whether the undo and redo operations can be executed.
The indent() function is used to reindent a paragraph. It is useful for code editors, for example in Qt Designer's code editor Ctrl+I invokes the indent() function.
Loading and saving text is achieved using setText() and text(), for example:
QFile file( fileName ); // Read the text from a file if ( file.open( IO_ReadOnly ) ) { QTextStream ts( &file ); textEdit->setText( ts.read() ); }
QFile file( fileName ); // Write the text to a file if ( file.open( IO_WriteOnly ) ) { QTextStream ts( &file ); ts << textEdit->text(); textEdit->setModified( FALSE ); }
By default the text edit wraps words at whitespace to fit within the text edit widget. The setWordWrap() function is used to specify the kind of word wrap you want, or NoWrap if you don't want any wrapping. Call setWordWrap() to set a fixed pixel width FixedPixelWidth, or character column (e.g. 80 column) FixedColumnWidth with the pixels or columns specified with setWrapColumnOrWidth(). If you use word wrap to the widget's width WidgetWidth, you can specify whether to break on whitespace or anywhere with setWrapPolicy().
The background color is set differently than other widgets, using setPaper(). You specify a brush style which could be a plain color or a complex pixmap.
Hypertext links are automatically underlined; this can be changed with setLinkUnderline(). The tab stop width is set with setTabStopWidth().
The zoomIn() and zoomOut() functions can be used to resize the text by increasing (decreasing for zoomOut()) the point size used. Images are not affected by the zoom functions.
The lines() function returns the number of lines in the text and paragraphs() returns the number of paragraphs. The number of lines within a particular paragraph is returned by linesOfParagraph(). The length of the entire text in characters is returned by length().
You can scroll to an anchor in the text, e.g. <a name="anchor"> with scrollToAnchor(). The find() function can be used to find and select a given string within the text.
The list of key-bindings which are implemented for editing:
Keypresses | Action |
---|---|
Backspace | Delete the character to the left of the cursor |
Delete | Delete the character to the right of the cursor |
Ctrl+A | Move the cursor to the beginning of the line |
Ctrl+B | Move the cursor one character left |
Ctrl+C | Copy the marked text to the clipboard (also Ctrl+Insert under Windows) |
Ctrl+D | Delete the character to the right of the cursor |
Ctrl+E | Move the cursor to the end of the line |
Ctrl+F | Move the cursor one character right |
Ctrl+H | Delete the character to the left of the cursor |
Ctrl+K | Delete to end of line |
Ctrl+N | Move the cursor one line down |
Ctrl+P | Move the cursor one line up |
Ctrl+V | Paste the clipboard text into line edit (also Shift+Insert under Windows) |
Ctrl+X | Cut the marked text, copy to clipboard (also Shift+Delete under Windows) |
Ctrl+Z | Undo the last operation |
Ctrl+Y | Redo the last operation |
LeftArrow | Move the cursor one character left |
Ctrl+LeftArrow | Move the cursor one word left |
RightArrow | Move the cursor one character right |
Ctrl+RightArrow | Move the cursor one word right |
UpArrow | Move the cursor one line up |
Ctrl+UpArrow | Move the cursor one word up |
DownArrow | Move the cursor one line down |
Ctrl+Down Arrow | Move the cursor one word down |
PageUp | Move the cursor one page up |
PageDown | Move the cursor one page down |
Home | Move the cursor to the beginning of the line |
Ctrl+Home | Move the cursor to the beginning of the text |
End | Move the cursor to the end of the line |
Ctrl+End | Move the cursor to the end of the text |
Shift+Wheel | Scroll the page horizontally (the Wheel is the mouse wheel) |
Ctrl+Wheel | Zoom the text |
To select (mark) text hold down the Shift key whilst pressing one of the movement keystrokes, for example, Shift+Right Arrow will select the character to the right, and Shift+Ctrl+Right Arrow will select the word to the right, etc.
By default the text edit widget operates in insert mode so all text that the user enters is inserted into the text edit and any text to the right of the cursor is moved out of the way. The mode can be changed to overwrite, where new text overwrites any text to the right of the cursor, using setOverwriteMode().
QTextEdit can also be used as read-only text viewer. Call setReadOnly( TRUE ) to disable editing. A read-only QTextEdit provides the same functionality as the (obsolete) QTextView. (QTextView is still supplied for compatibility with old code.)
When QTextEdit is used read-only the key-bindings are limited to navigation, and text may only be selected with the mouse:
Keypresses | Action |
---|---|
UpArrow | Move one line up |
DownArrow | Move one line down |
LeftArrow | Move one character left |
RightArrow | Move one character right |
PageUp | Move one (viewport) page up |
PageDown | Move one (viewport) page down |
Home | Move to the beginning of the text |
End | Move to the end of the text |
Shift+Wheel | Scroll the page horizontally (the Wheel is the mouse wheel) |
Ctrl+Wheel | Zoom the text |
The text edit may be able to provide some meta-information. For example, the documentTitle() function will return the text from within HTML <title> tags.
The text displayed in a text edit has a context. The context is a path which the text edit's QMimeSourceFactory uses to resolve the locations of files and images. It is passed to the mimeSourceFactory() when quering data. (See QTextEdit() and context().)
Note that we do not intend to add a full-featured web browser widget to Qt (because that would easily double Qt's size and only a few applications would benefit from it). The rich text support in Qt is designed to provide a fast, portable and efficient way to add reasonable online help facilities to applications, and to provide a basis for rich text editors.
See also Basic Widgets and Text Related Classes.
This enum is used by moveCursor() to specify in which direction the cursor should be moved:
This enum is used by doKeyboardAction() to specify which action should be executed:
This enum is used to set the vertical alignment of the text.
This enum defines the QTextEdit's word wrap modes. The following values are valid:
See also wordWrap and wordWrap.
This enum defines where text can be wrapped in word wrap mode.
The following values are valid:
See also wrapPolicy.
The context is a path which the text edit's QMimeSourceFactory uses to resolve the locations of files and images. It is passed to the mimeSourceFactory() when quering data.
For example if the text contains an image tag, <img src="image.png">, and the context is "path/to/look/in", the QMimeSourceFactory will try to load the image from "path/to/look/in/image.png". If the tag was <img src="/image.png">, the context will not be used (because QMimeSourceFactory recognizes that we have used an absolute path) and will try to load "/image.png". The context is applied in exactly the same way to hrefs, for example, <a href="target.html">Target</a>, would resolve to "path/to/look/in/target.html".
See also setAlignment().
Examples: network/clientserver/client/client.cpp, network/clientserver/server/server.cpp, network/httpd/httpd.cpp and process/process.cpp.
See also setBold().
See also cut(), removeSelectedText() and text.
See also setColor() and paper.
See also text.
Examples: helpviewer/helpwindow.cpp and qdir/qdir.cpp.
See also hasSelectedText and copyAvailable().
This signal is emitted when text is selected or de-selected in the text edit.
When text is selected this signal will be emitted with yes set to TRUE. If no text has been selected or if the selected text is de-selected this signal is emitted with yes set to FALSE.
If yes is TRUE then copy() can be used to copy the selection to the clipboard. If yes is FALSE then copy() does nothing.
See also selectionChanged().
This function is called to create a right mouse button popup menu at the document position pos. If you want to create a custom popup menu, reimplement this function and return the created popup menu. Ownership of the popup menu is transferred to the caller.
This function is called to create a right mouse button popup menu. If you want to create a custom popup menu, reimplement this function and return the created popup menu. Ownership of the popup menu is transferred to the caller.
This signal is emitted if the alignment of the current paragraph has changed.
The new alignment is a.
See also setAlignment().
This signal is emitted if the color of the current format has changed.
The new color is c.
See also setColor().
This signal is emitted if the font of the current format has changed.
The new font is f.
See also setCurrentFont().
This signal is emitted if the vertical alignment of the current format has changed.
The new vertical alignment is a.
See also setVerticalAlignment().
This signal is emitted if the position of the cursor changed. c points to the text cursor object.
See also setCursorPosition().
This signal is emitted if the position of the cursor changed. para contains the paragraph index and pos contains the character position within the paragraph.
See also setCursorPosition().
If there is no selected text (in selection 0) nothing happens.
See also QTextEdit::copy(), paste() and pasteSubType().
See also removeSelectedText() and cut().
Returns the title of the document parsed from the text. See the "documentTitle" property for details.
See also setCursorPosition().
See also setFamily(), setCurrentFont() and setPointSize().
If para and index are both null the search begins from the start of the text. If para and index are both not null, the search begins from the *index character position in the *para paragraph.
If cs is TRUE the search is case sensitive, otherwise it is case insensitive. If wo is TRUE the search looks for whole word matches only; otherwise it searches for any matching text. If forward is TRUE (the default) the search works forward from the starting position to the end of the text, otherwise it works backwards to the beginning of the text.
If expr is found the function returns TRUE. If index and para are not null, the number of the paragraph in which the first character of the match was found is put into *para, and the index position of that character within the paragraph is put into *index.
If expr is not found the function returns FALSE. If index and para are not null and expr is not found, *index and *para are undefined.
See also setCurrentFont(), setFamily() and setPointSize().
Examples: action/application.cpp, application/application.cpp, mdi/application.cpp and qwerty/qwerty.cpp.
See also setCursorPosition().
Returns FALSE if para or index is out of range otherwise returns TRUE.
Returns FALSE if para is out of range otherwise returns TRUE.
If there is no selection, *paraFrom, *indexFrom, *paraTo and *indexTo are all set to -1.
paraFrom, indexFrom, paraTo and indexTo must be non-null int pointers.
The selNum is the number of the selection (multiple selections are supported). It defaults to 0 (the default selection).
See also setSelection() and selectedText.
Returns TRUE if some text is selected in selection 0; otherwise returns FALSE. See the "hasSelectedText" property for details.
Reimplemented from QWidget.
See also paste() and pasteSubType().
Returns TRUE if the document has been modified by the user; otherwise returns FALSE. See the "modified" property for details.
Returns the text edit's overwrite mode. See the "overwriteMode" property for details.
Returns TRUE if the text edit is read-only; otherwise returns FALSE. See the "readOnly" property for details.
Returns TRUE if undo/redo is enabled; otherwise returns FALSE. See the "undoRedoEnabled" property for details.
See also setItalic().
Reimplemented from QWidget.
Reimplemented in QTextBrowser.
Returns the number of characters in the text. See the "length" property for details.
Warning: This function may be slow. Lines change all the time during word wrapping, so this function has to iterate over all the paragraphs and get the number of lines from each one individually.
Examples: action/application.cpp and application/application.cpp.
Returns TRUE if hypertext links will be underlined; otherwise returns FALSE. See the "linkUnderline" property for details.
See also setMimeSourceFactory().
Examples: helpviewer/helpwindow.cpp and qdir/qdir.cpp.
This signal is emitted when the modification of the document changed. If m is TRUE, the document was modified, otherwise the modification state has been reset to unmodified.
See also modified.
Returns the background (paper) brush. See the "paper" property for details.
If there is no text in the clipboard nothing happens.
See also pasteSubType(), cut() and QTextEdit::copy().
If there is no text with format subtype in the clipboard nothing happens.
See also paste(), cut() and QTextEdit::copy().
See also setCursorPosition().
See also setFamily(), setCurrentFont() and setPointSize().
If there is no operation to redo, e.g. there is no redo step in the undo/redo history, nothing happens.
See also redoAvailable(), undo() and undoDepth.
This signal is emitted when the availability of redo changes. If yes is TRUE, then redo() will work until redoAvailable( FALSE ) is next emitted.
See also redo() and undoDepth.
See also selectedText and removeSelection().
See also removeSelectedText().
Although used extensively internally you shouldn't need to call this yourself.
This signal is emitted if the user pressed the Return or the Enter key.
See also selectedText.
Returns the selected text (from selection 0) or an empty string if there is no currently selected text (in selection 0). See the "selectedText" property for details.
This signal is emitted whenever the selection changes.
See also setSelection() and copyAvailable().
See also setParagType().
Reimplemented in QMultiLineEdit.
See also bold().
Example: action/actiongroup/editor.cpp.
Sets the font of the current format to f.
See also font(), setPointSize() and setFamily().
See also getCursorPosition().
See also family() and setCurrentFont().
See also italic().
Sets whether hypertext links will be underlined. See the "linkUnderline" property for details.
See also mimeSourceFactory().
Sets whether the document has been modified by the user to m. See the "modified" property for details.
Sets the text edit's overwrite mode to b. See the "overwriteMode" property for details.
Sets the background (paper) brush to pap. See the "paper" property for details.
See also setAlignment().
Note that if s is zero or negative, the behaviour of this function is not defined.
See also pointSize(), setCurrentFont() and setFamily().
Sets whether the text edit is read-only to b. See the "readOnly" property for details.
Uses the selection settings of selection selNum. If selNum is 0, this is the default selection.
The cursor is moved to the end of the selection if selNum is 0, otherwise the cursor position remains unchanged.
See also getSelection() and selectedText.
This only works for selNum > 0. The default selection (selNum == 0) gets its attributes from the colorGroup() of this widget.
See also styleSheet().
Sets the tab stop width in pixels to ts. See the "tabStopWidth" property for details.
Sets the text edit's text to txt. See the "text" property for details.
Changes the text of the text edit to the string text and the context to context. Any previous text is removed.
text may be interpreted either as plain text or as rich text, depending on the textFormat(). The default setting is AutoText, i.e. the text edit autodetects the format from text.
The optional context is a path which the text edit's QMimeSourceFactory uses to resolve the locations of files and images. (See QTextEdit::QTextEdit().) It is passed to the text edit's QMimeSourceFactory when quering data.
Note that the undo/redo history is cleared by this function.
See also text and textFormat.
Sets the text format: rich text, plain text or auto text to f. See the "textFormat" property for details.
See also underline().
Sets the depth of the undo history to d. See the "undoDepth" property for details.
Sets whether undo/redo is enabled to b. See the "undoRedoEnabled" property for details.
Sets the word wrap mode to mode. See the "wordWrap" property for details.
Sets the position (in pixels or columns depending on the wrap mode) where text will be wrapped. See the "wrapColumnOrWidth" property for details.
Sets the word wrap policy, at whitespace or anywhere to policy. See the "wrapPolicy" property for details.
See also setStyleSheet().
Example: helpviewer/helpwindow.cpp.
Returns the tab stop width in pixels. See the "tabStopWidth" property for details.
Returns the text edit's text. See the "text" property for details.
Returns the text of paragraph para.
If textFormat() is RichText the text will contain HTML formatting tags.
This signal is emitted whenever the text in the text edit changes.
Examples: helpviewer/helpwindow.cpp, qwerty/qwerty.cpp and rot13/rot13.cpp.
Returns the text edit's text cursor.
Warning: QTextCursor is not in the public API, but in special circumstances you might wish to use it.
Returns the text format: rich text, plain text or auto text. See the "textFormat" property for details.
See also setUnderline().
If there is no operation to undo, e.g. there is no undo step in the undo/redo history, nothing happens.
See also undoAvailable(), redo() and undoDepth.
This signal is emitted when the availability of undo changes. If yes is TRUE, then undo() will work until undoAvailable( FALSE ) is next emitted.
See also undo() and undoDepth.
Returns the depth of the undo history. See the "undoDepth" property for details.
Returns the word wrap mode. See the "wordWrap" property for details.
Returns the position (in pixels or columns depending on the wrap mode) where text will be wrapped. See the "wrapColumnOrWidth" property for details.
Returns the word wrap policy, at whitespace or anywhere. See the "wrapPolicy" property for details.
See also zoomOut().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Zooms in on the text by by making the base font size one point larger and recalculating all font sizes. This does not change the size of any images.
See also zoomOut().
See also zoomIn().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Zooms out on the text by by making the base font size one point smaller and recalculating all font sizes. This does not change the size of any images.
See also zoomIn().
This property holds the title of the document parsed from the text.
For PlainText the title will be an empty string. For RichText the title will be the text between the <title> tags, if present, otherwise an empty string.
Get this property's value with documentTitle().
This property holds whether some text is selected in selection 0.
Get this property's value with hasSelectedText().
This property holds the number of characters in the text.
Get this property's value with length().
This property holds whether hypertext links will be underlined.
If TRUE (the default) hypertext links will be displayed underlined. If FALSE links will not be displayed underlined.
Set this property's value with setLinkUnderline() and get this property's value with linkUnderline().
This property holds whether the document has been modified by the user.
Set this property's value with setModified() and get this property's value with isModified().
This property holds the text edit's overwrite mode.
If FALSE (the default) characters entered by the user are inserted with any characters to the right being moved out of the way. If TRUE, the editor is in overwrite mode, i.e. characters entered by the user overwrite any characters to the right of the cursor position.
Set this property's value with setOverwriteMode() and get this property's value with isOverwriteMode().
This property holds the background (paper) brush.
The brush that is currently used to draw the background of the text edit. The initial setting is an empty brush.
Set this property's value with setPaper() and get this property's value with paper().
This property holds whether the text edit is read-only.
In a read-only text edit the user can only navigate through the text and select text; modifying the text is not possible.
This property's default is FALSE.
Set this property's value with setReadOnly() and get this property's value with isReadOnly().
This property holds the selected text (from selection 0) or an empty string if there is no currently selected text (in selection 0).
The text is always returned as PlainText regardless of the text format. In a future version of Qt an HTML subset may be returned depending on the text format.
See also hasSelectedText.
Get this property's value with selectedText().
This property holds the tab stop width in pixels.
Set this property's value with setTabStopWidth() and get this property's value with tabStopWidth().
This property holds the text edit's text.
There is no default text.
On setting, any previous text is deleted.
The text may be interpreted either as plain text or as rich text, depending on the textFormat(). The default setting is AutoText, i.e. the text edit autodetects the format of the text.
For richtext, calling text() on an editable QTextEdit will cause the text to be regenerated from the textedit. This may mean that the QString returned may not be exactly the same as the one that was set.
See also textFormat.
Set this property's value with setText() and get this property's value with text().
This property holds the text format: rich text, plain text or auto text.
The text format is one of the following:
Set this property's value with setTextFormat() and get this property's value with textFormat().
This property holds the depth of the undo history.
The maximum number of steps in the undo/redo history. The default is 100.
Set this property's value with setUndoDepth() and get this property's value with undoDepth().
This property holds whether undo/redo is enabled.
The default is TRUE.
Set this property's value with setUndoRedoEnabled() and get this property's value with isUndoRedoEnabled().
This property holds the word wrap mode.
The default mode is WidgetWidth which causes words to be wrapped at the right edge of the text edit. Wrapping occurs at whitespace, keeping whole words intact. If you want wrapping to occur within words use setWrapPolicy(). If you set a wrap mode of FixedPixelWidth or FixedColumnWidth you should also call setWrapColumnOrWidth() with the width you want.
See also WordWrap, wrapColumnOrWidth and wrapPolicy.
Set this property's value with setWordWrap() and get this property's value with wordWrap().
This property holds the position (in pixels or columns depending on the wrap mode) where text will be wrapped.
If the wrap mode is FixedPixelWidth, the value is the number of pixels from the left edge of the text edit at which text should be wrapped. If the wrap mode is FixedColumnWidth, the value is the column number (in character columns) from the left edge of the text edit at which text should be wrapped.
See also wordWrap.
Set this property's value with setWrapColumnOrWidth() and get this property's value with wrapColumnOrWidth().
This property holds the word wrap policy, at whitespace or anywhere.
Defines where text can be wrapped when word wrap mode is not NoWrap. The choices are AtWhiteSpace (the default) and Anywhere.
See also wordWrap.
Set this property's value with setWrapPolicy() and get this property's value with wrapPolicy().
This file is part of the Qt toolkit. Copyright © 1995-2002 Trolltech. All Rights Reserved.
Copyright © 2002 Trolltech | Trademarks | Qt version 3.0.4
|