Qt 4.8: QTextBrowser Class Reference

Qt Reference Documentation

# QTextBrowser Class Reference

The QTextBrowser class provides a rich text browser with hypertext navigation. More...

     #include <QTextBrowser>

Inherits: QTextEdit.

- List of all members, including inherited members
- Qt 3 support members

## Properties

modified : const bool openExternalLinks : bool openLinks : bool readOnly : const bool | searchPaths : QStringList source : QUrl undoRedoEnabled : const bool

- 15 properties inherited from QTextEdit
- 2 properties inherited from QAbstractScrollArea
- 6 properties inherited from QFrame
- 58 properties inherited from QWidget
- 1 property inherited from QObject

## Public Functions

| QTextBrowser ( QWidget * parent = 0 )
int | backwardHistoryCount () const
void | clearHistory ()
int | forwardHistoryCount () const
QString | historyTitle ( int i ) const
QUrl | historyUrl ( int i ) const
bool | isBackwardAvailable () const
bool | isForwardAvailable () const
bool | openExternalLinks () const
bool | openLinks () const
QStringList | searchPaths () const
void | setOpenExternalLinks ( bool open )
void | setOpenLinks ( bool open )
void | setSearchPaths ( const QStringList & paths )
QUrl | source () const

## Reimplemented Public Functions

virtual QVariant | loadResource ( int type, const QUrl & name )

- 58 public functions inherited from QTextEdit
- 17 public functions inherited from QAbstractScrollArea
- 14 public functions inherited from QFrame
- 221 public functions inherited from QWidget
- 29 public functions inherited from QObject
- 13 public functions inherited from QPaintDevice

## Public Slots

virtual void | backward ()
virtual void | forward ()
virtual void | home ()
virtual void | reload ()
virtual void | setSource ( const QUrl & name )

- 25 public slots inherited from QTextEdit
- 19 public slots inherited from QWidget
- 1 public slot inherited from QObject

## Signals

void | anchorClicked ( const QUrl & link )
void | backwardAvailable ( bool available )
void | forwardAvailable ( bool available )
void | highlighted ( const QUrl & link )
void | highlighted ( const QString & link )
void | historyChanged ()
void | sourceChanged ( const QUrl & src )

- 7 signals inherited from QTextEdit
- 1 signal inherited from QWidget
- 1 signal inherited from QObject

## Reimplemented Protected Functions

virtual bool | event ( QEvent * e )
virtual bool | focusNextPrevChild ( bool next )
virtual void | focusOutEvent ( QFocusEvent * ev )
virtual void | keyPressEvent ( QKeyEvent * ev )
virtual void | mouseMoveEvent ( QMouseEvent * e )
virtual void | mousePressEvent ( QMouseEvent * e )
virtual void | mouseReleaseEvent ( QMouseEvent * e )
virtual void | paintEvent ( QPaintEvent * e )

- 25 protected functions inherited from QTextEdit
- 18 protected functions inherited from QAbstractScrollArea
- 3 protected functions inherited from QFrame
- 37 protected functions inherited from QWidget
- 8 protected functions inherited from QObject
- 1 protected function inherited from QPaintDevice

### Additional Inherited Members

- 4 static public members inherited from QWidget
- 7 static public members inherited from QObject
- 25 protected functions inherited from QTextEdit
- 18 protected functions inherited from QAbstractScrollArea
- 3 protected functions inherited from QFrame
- 37 protected functions inherited from QWidget
- 8 protected functions inherited from QObject
- 1 protected function inherited from QPaintDevice
- 1 protected slot inherited from QAbstractScrollArea
- 1 protected slot inherited from QWidget

## Detailed Description

The QTextBrowser class provides a rich text browser with hypertext navigation.

This class extends QTextEdit (in read-only mode), adding some navigation functionality so that users can follow links in hypertext documents.

If you want to provide your users with an editable rich text editor, use QTextEdit. If you want a text browser without hypertext navigation use QTextEdit, and use QTextEdit::setReadOnly() to disable editing. If you just need to display a small piece of rich text use QLabel.

### Document Source and Contents

The contents of QTextEdit are set with setHtml() or setPlainText(), but QTextBrowser also implements the setSource() function, making it possible to use a named document as the source text. The name is looked up in a list of search paths and in the directory of the current document factory.

If a document name ends with an anchor (for example, "`#anchor"`), the text browser automatically scrolls to that position (using scrollToAnchor()). When the user clicks on a hyperlink, the browser will call setSource() itself with the link's `href` value as argument. You can track the current source by connecting to the sourceChanged() signal.

### Navigation

QTextBrowser provides backward() and forward() slots which you can use to implement Back and Forward buttons. The home() slot sets the text to the very first document displayed. The anchorClicked() signal is emitted when the user clicks an anchor. To override the default navigation behavior of the browser, call the setSource() function to supply new document text in a slot connected to this signal.

If you want to load documents stored in the Qt resource system use `qrc` as the scheme in the URL to load. For example, for the document resource path `:/docs/index.html` use `qrc:/docs/index.html` as the URL with setSource(). To access local files, use `file` as the scheme in the URL.

See also QTextEdit and QTextDocument.

## Property Documentation

### modified : const bool

This property holds whether the contents of the text browser have been modified.

### openExternalLinks : bool

Specifies whether QTextBrowser should automatically open links to external sources using QDesktopServices::openUrl() instead of emitting the anchorClicked signal. Links are considered external if their scheme is neither file or qrc.

The default value is false.

This property was introduced in Qt 4.2.

Access functions:

bool | openExternalLinks () const
void | setOpenExternalLinks ( bool open )

### openLinks : bool

This property specifies whether QTextBrowser should automatically open links the user tries to activate by mouse or keyboard.

Regardless of the value of this property the anchorClicked signal is always emitted.

The default value is true.

This property was introduced in Qt 4.3.

Access functions:

bool | openLinks () const
void | setOpenLinks ( bool open )

### readOnly : const bool

This property holds whether the text browser is read-only.

By default, this property is true.

### searchPaths : QStringList

This property holds the search paths used by the text browser to find supporting content.

QTextBrowser uses this list to locate images and documents.

By default, this property contains an empty string list.

Access functions:

QStringList | searchPaths () const
void | setSearchPaths ( const QStringList & paths )

### source : QUrl

This property holds the name of the displayed document.

This is a an invalid url if no document is displayed or if the source is unknown.

When setting this property QTextBrowser tries to find a document with the specified name in the paths of the searchPaths property and directory of the current source, unless the value is an absolute file path. It also checks for optional anchors and scrolls the document accordingly

If the first tag in the document is `<qt type=detail>`, the document is displayed as a popup rather than as new document in the browser window itself. Otherwise, the document is displayed normally in the text browser with the text set to the contents of the named document with setHtml().

By default, this property contains an empty URL.

Access functions:

QUrl | source () const
virtual void | setSource ( const QUrl & name )

### undoRedoEnabled : const bool

This property holds whether the text browser supports undo/redo operations.

By default, this property is false.

## Member Function Documentation

### QTextBrowser::QTextBrowser ( QWidget * parent = 0 )

Constructs an empty QTextBrowser with parent parent.

### void QTextBrowser::anchorClicked ( const QUrl & link )`[signal]`

This signal is emitted when the user clicks an anchor. The URL referred to by the anchor is passed in link.

Note that the browser will automatically handle navigation to the location specified by link unless the openLinks property is set to false or you call setSource() in a slot connected. This mechanism is used to override the default navigation features of the browser.

### void QTextBrowser::backward ()`[virtual slot]`

Changes the document displayed to the previous document in the list of documents built by navigating links. Does nothing if there is no previous document.

See also forward() and backwardAvailable().

### void QTextBrowser::backwardAvailable ( bool available )`[signal]`

This signal is emitted when the availability of backward() changes. available is false when the user is at home(); otherwise it is true.

### int QTextBrowser::backwardHistoryCount () const

Returns the number of locations backward in the history.

This function was introduced in Qt 4.4.

### void QTextBrowser::clearHistory ()

Clears the history of visited documents and disables the forward and backward navigation.

This function was introduced in Qt 4.2.

See also backward() and forward().

### bool QTextBrowser::event ( QEvent * e )`[virtual protected]`

Reimplemented from QObject::event().

### bool QTextBrowser::focusNextPrevChild ( bool next )`[virtual protected]`

Reimplemented from QWidget::focusNextPrevChild().

### void QTextBrowser::focusOutEvent ( QFocusEvent * ev )`[virtual protected]`

Reimplemented from QWidget::focusOutEvent().

### void QTextBrowser::forward ()`[virtual slot]`

Changes the document displayed to the next document in the list of documents built by navigating links. Does nothing if there is no next document.

See also backward() and forwardAvailable().

### void QTextBrowser::forwardAvailable ( bool available )`[signal]`

This signal is emitted when the availability of forward() changes. available is true after the user navigates backward() and false when the user navigates or goes forward().

### int QTextBrowser::forwardHistoryCount () const

Returns the number of locations forward in the history.

This function was introduced in Qt 4.4.

### void QTextBrowser::highlighted ( const QUrl & link )`[signal]`

This signal is emitted when the user has selected but not activated an anchor in the document. The URL referred to by the anchor is passed in link.

### void QTextBrowser::highlighted ( const QString & link )`[signal]`

This is an overloaded function.

Convenience signal that allows connecting to a slot that takes just a QString, like for example QStatusBar's message().

### void QTextBrowser::historyChanged ()`[signal]`

This signal is emitted when the history changes.

This function was introduced in Qt 4.4.

See also historyTitle() and historyUrl().

### QString QTextBrowser::historyTitle ( int i ) const

Returns the documentTitle() of the HistoryItem.

Input | Return
--- | ---
i < 0 | backward() history
i == 0 | current, see QTextBrowser::source()
i > 0 | forward() history

     backaction.setToolTip(browser.historyTitle(-1));
     forwardaction.setToolTip(browser.historyTitle(+1));

This function was introduced in Qt 4.4.

### QUrl QTextBrowser::historyUrl ( int i ) const

Returns the url of the HistoryItem.

Input | Return
--- | ---
i < 0 | backward() history
i == 0 | current, see QTextBrowser::source()
i > 0 | forward() history

This function was introduced in Qt 4.4.

### void QTextBrowser::home ()`[virtual slot]`

Changes the document displayed to be the first document from the history.

### bool QTextBrowser::isBackwardAvailable () const

Returns true if the text browser can go backward in the document history using backward().

This function was introduced in Qt 4.2.

See also backwardAvailable() and backward().

### bool QTextBrowser::isForwardAvailable () const

Returns true if the text browser can go forward in the document history using forward().

This function was introduced in Qt 4.2.

See also forwardAvailable() and forward().

### void QTextBrowser::keyPressEvent ( QKeyEvent * ev )`[virtual protected]`

Reimplemented from QWidget::keyPressEvent().

The event ev is used to provide the following keyboard shortcuts:

Keypress | Action
--- | ---
Alt+Left Arrow | backward()
Alt+Right Arrow | forward()
Alt+Up Arrow | home()

### QVariant QTextBrowser::loadResource ( int type, const QUrl & name )`[virtual]`

Reimplemented from QTextEdit::loadResource().

This function is called when the document is loaded and for each image in the document. The type indicates the type of resource to be loaded. An invalid QVariant is returned if the resource cannot be loaded.

The default implementation ignores type and tries to locate the resources by interpreting name as a file name. If it is not an absolute path it tries to find the file in the paths of the searchPaths property and in the same directory as the current source. On success, the result is a QVariant that stores a QByteArray with the contents of the file.

If you reimplement this function, you can return other QVariant types. The table below shows which variant types are supported depending on the resource type:

ResourceType | QVariant::Type
--- | ---
QTextDocument::HtmlResource | QString or QByteArray
QTextDocument::ImageResource | QImage, QPixmap or QByteArray
QTextDocument::StyleSheetResource | QString or QByteArray

### void QTextBrowser::mouseMoveEvent ( QMouseEvent * e )`[virtual protected]`

Reimplemented from QWidget::mouseMoveEvent().

### void QTextBrowser::mousePressEvent ( QMouseEvent * e )`[virtual protected]`

Reimplemented from QWidget::mousePressEvent().

### void QTextBrowser::mouseReleaseEvent ( QMouseEvent * e )`[virtual protected]`

Reimplemented from QWidget::mouseReleaseEvent().

### void QTextBrowser::paintEvent ( QPaintEvent * e )`[virtual protected]`

Reimplemented from QWidget::paintEvent().

### void QTextBrowser::reload ()`[virtual slot]`

Reloads the current set source.

### void QTextBrowser::sourceChanged ( const QUrl & src )`[signal]`

This signal is emitted when the source has changed, src being the new source.

Source changes happen both programmatically when calling setSource(), forward(), backword() or home() or when the user clicks on links or presses the equivalent key sequences.