Classes - Annotated - Tree - Functions - Home - Structure

QTextView Class Reference

The QTextView class provides a sophisticated rich text viewer. More...

#include <qtextview.h>

Inherits QScrollView.

Inherited by QTextEdit and QTextBrowser.

List of all member functions.

Public Members

Public Slots

Signals

Properties

Protected Members

Protected Slots


Detailed Description

The QTextView class provides a sophisticated rich text viewer.

QTextView is a widget which can show large amounts of uneditable text with varying fonts and font attributes, e.g. different point sizes, colors, bold, italic, etc. Paragraphs can have different alignments and word-wrap policies. QTextView can also show images (using QMimeSourceFactory), lists and tables. If the text is too large to view within the text view's viewport, scrollbars will appear. The text view can load both plain text and HTML files (a subset of HTML 4 and CSS1 is supported). The rendering style and valid tags are defined by a styleSheet(). Currently a subset of HTML 3.2 and 4, and CSS1 is supported, see setStyleSheet() and QStyleSheet for details. The images identified by image tags are displayed if they can be interpreted using the text view's QMimeSourceFactory. (See setMimeSourceFactory() for details.)

If you want to provide your users with editable rich text use QTextEdit. 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.

Set the text with setText(). You can include rich text directly in your source code since it is based on HTML tags, e.g.

        textView->setText( "<font color=\"red\">Red</font>" );
    
You can append text with append(), for example to display an output log.

By default the text view will try to guess the format of the text (plain text or HTML) and display accordingly, but you can control this with setTextFormat().

By default the text view wraps words at whitespace to fit within the text view 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 by one point per call. Images are not affected.

The entire text is returned by text(), and the text of a particular paragraph by text(int).

The user can select text in the text view using the mouse. If they've selected text hasSelectedText() will return TRUE. The selection's position can be obtained with getSelection(), and the selection itself is returned by selectedText(). The selection can be copied to the clipboard with copy(). The entire text can be selected (or deselected) with selectAll().

The lines() function returns the number of lines in the text, 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 user can navigate the text view by using the scrollbars and by clicking hypertext links with the mouse. The following keyboard shortcuts are also supported:

The text view may be able to provide some meta-information. The documentTitle() function will return the text from within HTML <title> tags.

The text displayed in a text view has a context. The context is a path which the text view's QMimeSourceFactory uses to resolve the locations of files and images. It is passed to the mimeSourceFactory() when quering data. (See QTextView() 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). In particular, the rich text support in Qt is designed to provide a fast, portable and efficient way to add reasonable online help facilities to applications.


Member Type Documentation

QTextView::WordWrap

This enum defines the QTextView's word wrap modes. The following values are valid:

See also wordWrap and wordWrap.

QTextView::WrapPolicy

This enum defines where text can be wrapped in word wrap mode.

The following values are valid:

See also wrapPolicy.


Member Function Documentation

QTextView::QTextView ( const QString & text, const QString & context = QString::null, QWidget * parent = 0, const char * name = 0 )

Constructs a QTextView displaying the text text with context context, with parent parent and name name.

The context is a path which the text view'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 given 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".

QTextView::QTextView ( QWidget * parent = 0, const char * name = 0 )

Constructs an empty QTextView with parent parent and name name.

QString QTextView::anchorAt ( const QPoint & pos )

If there is an anchor at position pos (in contents coordinates), its name is returned, otherwise an empty string is returned.

void QTextView::append ( const QString & text ) [virtual slot]

Appends the text text to the end of the text view.

Examples: clientserver/client/client.cpp, clientserver/server/server.cpp, httpd/httpd.cpp, mdi/application.cpp and process/process.cpp.

QString QTextView::context () const

Returns the context of the view.

The context is a path which the text view's QMimeSourceFactory uses to resolve the locations of files and images.

See also text and text.

Examples: helpviewer/helpwindow.cpp and qdir/qdir.cpp.

void QTextView::copy () [virtual slot]

Copies any selected text to the clipboard.

See also hasSelectedText and copyAvailable().

void QTextView::copyAvailable ( bool yes ) [signal]

This signal is emitted when text is selected or deselected in the text view.

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

QPopupMenu * QTextView::createPopupMenu () [virtual protected]

This function is called to create the popup menu which is shown when the user clicks on the text view with the right mouse button. 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.

QTextDocument * QTextView::document () const [protected]

This function returns the QTextDocument which is used by the text view.

QTextDocument is not in the public API and its API might change in an incompatible manner in the future.

QString QTextView::documentTitle () const

Returns the title of the document parsef from the text. See the "documentTitle" property for details.

bool QTextView::find ( const QString & expr, bool cs, bool wo, bool forward = TRUE, int * para = 0, int * index = 0 ) [virtual]

Finds the next occurrence of the string, expr, starting from character position index within paragraph para.

If para and index are both 0 the search begins from the start of the text. 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 and overwrites para with the number of the paragraph in which the first character of the match was found and index with the index position of that character within the para.

If expr is not found the function returns FALSE and the contents of index and para are undefined.

bool QTextView::focusNextPrevChild ( bool n ) [virtual protected]

Reimplemented to allow tabbing through links

Reimplemented from QScrollView.

void QTextView::getSelection ( int * paraFrom, int * indexFrom, int * paraTo, int * indexTo, int selNum = 0 ) const

If there is a selection paraFrom is set to the number of the paragraph in which the selection begins and paraTo is set to the number of the paragraph in which the selection ends. (They could be the same.) indexFrom is set to the index at which the selection begins within paraFrom, and indexTo is set to the index at which the selection ends within paraTo.

If there is no selection, paraFrom, indexFrom, paraTo and indexTo are all set to -1.

The selNum is the number of the selection (since multiple selections are supported). It defaults to 0 (the first selection).

See also selectedText.

bool QTextView::hasSelectedText () const

Returns TRUE whether some text is selected, otherwise returns FALSE. See the "hasSelectedText" property for details.

int QTextView::heightForWidth ( int w ) const [virtual]

Returns how many pixels high the text view needs to be to display the text if the text view is w pixels wide.

Reimplemented from QWidget.

void QTextView::keyPressEvent ( QKeyEvent * e ) [virtual protected]

Processes the key event, e. By default key events are used to provide keyboard navigation.

Reimplemented from QWidget.

Reimplemented in QTextBrowser.

int QTextView::length () const

Returns the total number of characters in the text. See the "length" property for details.

int QTextView::lineOfChar ( int para, int index )

Returns the line number of the line in paragraph para in which the character at position index appears. The index position is relative to the beginning of the paragraph. If there is no such paragraph or no such character at the index position (e.g. the index is out of range) -1 is returned.

int QTextView::lines () const

Returns the number of lines in the text view.

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.

int QTextView::linesOfParagraph ( int para ) const

Returns the number of lines in paragraph para.

bool QTextView::linkUnderline () const

Returns TRUE whether links will be underlined, otherwise returns FALSE. See the "linkUnderline" property for details.

QMimeSourceFactory * QTextView::mimeSourceFactory () const

Returns the QMimeSourceFactory which is currently used by this text view.

See also setMimeSourceFactory().

Examples: helpviewer/helpwindow.cpp and qdir/qdir.cpp.

QBrush QTextView::paper () const

Returns the background (paper) brush. See the "paper" property for details.

int QTextView::paragraphs () const

Returns the number of paragraphs in the text.

void QTextView::repaintChanged () [protected]

Repaints any paragraphs that have changed.

Although used extensively internally you shouldn't need to call it yourself.

void QTextView::scrollToAnchor ( const QString & name ) [virtual slot]

Scrolls the text view to make the text at the anchor called name visible, if it can be found in the document. If the anchor isn't found no scrolling will occur. An anchor is defined using the HTML anchor tag, e.g. <a name="target">.

void QTextView::selectAll ( bool select = TRUE ) [virtual slot]

If select is TRUE (the default), all the text is selected. If select is FALSE any selected text is deselected (i.e. the selection is cleared).

QString QTextView::selectedText () const

Returns the selected text or an empty string if there is no currently selected text. See the "selectedText" property for details.

void QTextView::selectionChanged () [signal]

This signal is emitted whenever the selection changes.

See also copyAvailable().

void QTextView::setDocument ( QTextDocument * doc ) [protected]

This function sets the QTextDocument which should be used by the text view. This can be used, for example, if you want to display a document using multiple views. You would create a QTextDocument and set it to the text views which should display it. You would need to connect to the textChanged() and selectionChanged() signals of all the text views and update them all accordingly (preferably with a slight delay for efficiency reasons).

QTextDocument is not in the public API and its API might change in an incompatible manner in the future.

void QTextView::setLinkUnderline ( bool ) [virtual slot]

Sets whether links will be underlined. See the "linkUnderline" property for details.

void QTextView::setMimeSourceFactory ( QMimeSourceFactory * factory ) [virtual slot]

Sets the text view's mimesource factory to factory. See QMimeSourceFactory for further details.

See also mimeSourceFactory().

void QTextView::setPaper ( const QBrush & pap ) [virtual slot]

Sets the background (paper) brush to pap. See the "paper" property for details.

void QTextView::setStyleSheet ( QStyleSheet * styleSheet ) [virtual slot]

Sets the stylesheet to use with this text view to styleSheet.

See also styleSheet().

void QTextView::setTabStopWidth ( int ts ) [virtual slot]

Sets the tab width used by the text view to ts.

See also tabStopWidth().

void QTextView::setText ( const QString & txt ) [slot]

Sets the text view's text to txt. See the "text" property for details.

void QTextView::setText ( const QString & text, const QString & context ) [virtual slot]

Changes the text of the view 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 view autodetects the format from text.

The optional context is a path which the text view's QMimeSourceFactory uses to resolve the locations of files and images. (See QTextView::QTextView().) It is passed to the text view's QMimeSourceFactory when quering data.

See also text and textFormat.

void QTextView::setTextFormat ( TextFormat f ) [virtual slot]

Sets the text format: rich text, plain text or auto text to f. See the "textFormat" property for details.

void QTextView::setWordWrap ( WordWrap mode ) [virtual slot]

Sets the word wrap mode to mode. See the "wordWrap" property for details.

void QTextView::setWrapColumnOrWidth ( int ) [virtual slot]

Sets the position (in pixels or columns depending on the wrap mode) where text will be wrapped. See the "wrapColumnOrWidth" property for details.

void QTextView::setWrapPolicy ( WrapPolicy policy ) [virtual slot]

Sets the word wrap policy, at whitespace or anywhere to policy. See the "wrapPolicy" property for details.

QStyleSheet * QTextView::styleSheet () const

Returns the QStyleSheet which is currently used in this text view.

See also setStyleSheet().

Example: helpviewer/helpwindow.cpp.

int QTextView::tabStopWidth () const

Returns the tab width used by the text view.

See also setTabStopWidth().

QString QTextView::text () const

Returns the text view's text. See the "text" property for details.

QString QTextView::text ( int para ) const

Returns the text of the paragraph para.

If textFormat() is RichText the text will contain HTML formatting tags.

void QTextView::textChanged () [signal]

This signal is emitted whenever the text in the text view changes.

See also text and append().

Examples: helpviewer/helpwindow.cpp, qwerty/qwerty.cpp and rot13/rot13.cpp.

TextFormat QTextView::textFormat () const

Returns the text format: rich text, plain text or auto text. See the "textFormat" property for details.

void QTextView::updateStyles () [protected]

Updates all the rendering styles used to display the text. You will probably want to call this function after calling setStyleSheet().

WordWrap QTextView::wordWrap () const

Returns the word wrap mode. See the "wordWrap" property for details.

int QTextView::wrapColumnOrWidth () const

Returns the position (in pixels or columns depending on the wrap mode) where text will be wrapped. See the "wrapColumnOrWidth" property for details.

WrapPolicy QTextView::wrapPolicy () const

Returns the word wrap policy, at whitespace or anywhere. See the "wrapPolicy" property for details.

void QTextView::zoomIn ( int range ) [virtual slot]

Zooms in on the text by by making the standard font size range points larger and recalculating all font sizes. This does not change the size of any images.

See also zoomOut().

void QTextView::zoomIn () [virtual slot]

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 standard font size one point larger and recalculating all font sizes. This does not change the size of any images.

See also zoomOut().

void QTextView::zoomOut ( int range ) [virtual slot]

Zooms out on the text by making the standard font size range points smaller and recalculating all font sizes. This does not change the size of any images.

See also zoomIn().

void QTextView::zoomOut () [virtual slot]

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 standard font size one point smaller and recalculating all font sizes. This does not change the size of any images.

See also zoomIn().


Property Documentation

QString documentTitle

This property holds the title of the document parsef 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().

bool hasSelectedText

This property holds whether some text is selected.

Get this property's value with hasSelectedText().

int length

This property holds the total number of characters in the text.

Returns the total number of characters of the text.

Get this property's value with length().

bool linkUnderline

This property holds whether links will be underlined.

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

QBrush paper

This property holds the background (paper) brush.

The brush that is currently used to draw the background of the text view. The initial setting is an empty brush.

Set this property's value with setPaper() and get this property's value with paper().

QString selectedText

This property holds the selected text or an empty string if there is no currently selected text.

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.

Get this property's value with selectedText().

See also hasSelectedText.

QString text

This property holds the text view's text.

The current text in the text view.

On setting, any previous text is deleted.

The value may be interpreted either as plain text or as rich text, depending on the textFormat(). The default setting is AutoText, i.e. the text view autodetects the format of the text.

Set this property's value with setText() and get this property's value with text().

See also textFormat.

TextFormat textFormat

This property holds the text format: rich text, plain text or auto text.

The current text format.

Possible values are:

Set this property's value with setTextFormat() and get this property's value with textFormat().

WordWrap wordWrap

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 view. Wrapping occurs at spaces, keeping whole words intact. If you want wrapping to occur within words use wrapPolicy. If you set a wrap mode of FixedPixelWidth or FixedColumnWidth you should also set wrapColumnOrWidth to the width you want.

Set this property's value with setWordWrap() and get this property's value with wordWrap().

See also QTextView::WordWrap, wrapColumnOrWidth and wrapPolicy.

int wrapColumnOrWidth

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 view 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 view at which text should be wrapped.

Set this property's value with setWrapColumnOrWidth() and get this property's value with wrapColumnOrWidth().

See also wordWrap.

WrapPolicy wrapPolicy

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.

Set this property's value with setWrapPolicy() and get this property's value with wrapPolicy().

See also wordWrap.


Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):


This file is part of the Qt toolkit, copyright © 1995-2000 Trolltech, all rights reserved.


Copyright © 2000 TrolltechTrademarks
Qt version main-beta1