QTextBrowser Class

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

Header: #include <QTextBrowser>
qmake: QT += widgets
Inherits: QTextEdit

Properties

Public Functions

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
QTextDocument::ResourceType sourceType() const

Public Slots

void setSource(const QUrl &name, QTextDocument::ResourceType type)
virtual void setSource(const QUrl &name)

Detailed Description

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.

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

See also QTextEdit and QTextDocument.

Property Documentation

modified : const bool

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

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)

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 QTextDocument::setHtml() or QTextDocument::setMarkdown(), depending on whether the filename ends with any of the known Markdown file extensions.

If you would like to avoid automatic type detection and specify the type explicitly, call setSource() rather than setting this property.

By default, this property contains an empty URL.

Access functions:

QUrl source() const
virtual void setSource(const QUrl &name)
void setSource(const QUrl &name, QTextDocument::ResourceType type)

sourceType : const QTextDocument::ResourceType

This property holds the type of the displayed document

This is QTextDocument::UnknownResource if no document is displayed or if the type of the source is unknown. Otherwise it holds the type that was detected, or the type that was specified when setSource() was called.

Access functions:

QTextDocument::ResourceType sourceType() const

undoRedoEnabled : const bool

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

By default, this property is false.