6.0.0-rc4Source

QTextBrowser Class

The QTextBrowser class shows documents that the user can go back and forward through. More…

Header#include <QTextBrowser>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes
InheritsQTextEdit

List of all members, including inherited members

Public Functions

QTextBrowser(QWidget *parent = nullptr)
~QTextBrowser()
QUrlsource() const
QStringListsearchPaths() const
voidsetSearchPaths(const QStringList &paths)
virtual QVariantloadResource(int type, const QUrl &name)
boolisBackwardAvailable() const
boolisForwardAvailable() const
voidclearHistory()
QStringhistoryTitle(int i) const
QUrlhistoryUrl(int i) const
intbackwardHistoryCount() const
intforwardHistoryCount() const
boolopenExternalLinks() const
voidsetOpenExternalLinks(bool open)
boolopenLinks() const
voidsetOpenLinks(bool open)

Public Slots

virtual voidsetSource(const QUrl &name)
virtual voidbackward()
virtual voidforward()
virtual voidhome()
virtual voidreload()

Signals

voidbackwardAvailable(bool available)
voidforwardAvailable(bool available)
voidhistoryChanged()
voidsourceChanged(const QUrl &src)
voidanchorClicked(const QUrl &link)
voidhighlighted(const QUrl &link)

Reimplemented Protected Functions

Detailed Description

A text browser is a read-only QTextEdit that loads its text from a source, a file name or a URL, and keeps a history of the sources it showed. A source that is HTML, by its name (.html, .htm) or by its contents, is shown as rich text, and a click on one of its links goes to the document that the link names:

auto *help = new QTextBrowser(window);
help->setSearchPaths({"/usr/share/myapp/help"});
help->setSource(QUrl("index.txt"));
connect(backButton, &QPushButton::clicked, help, &QTextBrowser::backward);

A relative source is looked up next to the current one, then in the search paths. Alt+Left and Alt+Right go backward and forward. A click on a link emits anchorClicked(); with openLinks(), which is the default, the browser then shows the target, if it is a local file or a relative name.

See also QTextEdit.

Differences from Qt

The HTML is what QTextDocument reads; there are no images and no Markdown. A link to somewhere else than a file (http, mailto) is never opened: openExternalLinks() is recorded only, and anchorClicked() tells about the click. loadResource() reads local files; the fragment of a source is ignored, so a link does not scroll to a place in a document. Links are not reached with the keys.

Member Function Documentation

[explicit] QTextBrowser::QTextBrowser(QWidget *parent = nullptr)

Constructs an empty, read-only text browser with the given parent.

[override] QTextBrowser::~QTextBrowser()

Destroys the text browser.

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

This signal is emitted when the user clicks a link; link is its target, as it is written in the document. With openLinks() the browser then shows it.

[slot, virtual] void QTextBrowser::backward()

Goes back to the source before the current one.

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

This signal is emitted when going backward becomes possible or not; available says which.

int QTextBrowser::backwardHistoryCount() const

Returns the number of sources to go backward to.

void QTextBrowser::clearHistory()

Forgets the history but for the source that is shown.

[slot, virtual] void QTextBrowser::forward()

Goes forward to the source after the current one.

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

This signal is emitted when going forward becomes possible or not; available says which.

int QTextBrowser::forwardHistoryCount() const

Returns the number of sources to go forward to.

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

This signal is emitted when the mouse comes over a link, with its target as link, and when it leaves the link, with an empty URL.

[signal] void QTextBrowser::historyChanged()

This signal is emitted when the history changes.

QString QTextBrowser::historyTitle(int i) const

Returns the title of the history entry i: a negative i is backward, 0 the current source, a positive one forward. The title is the file name, as documents have no titles in Nt.

QUrl QTextBrowser::historyUrl(int i) const

Returns the source of the history entry i, counted as by historyTitle(), or an empty URL.

[slot, virtual] void QTextBrowser::home()

Goes to the first source of the history.

bool QTextBrowser::isBackwardAvailable() const

Returns whether there is a source to go backward to.

bool QTextBrowser::isForwardAvailable() const

Returns whether there is a source to go forward to.

[override, protected] void QTextBrowser::keyPressEvent(QKeyEvent *event)

Reimplements QTextEdit::keyPressEvent().

Alt+Left goes backward and Alt+Right forward.

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

Returns the contents of the resource name as a QString, or an invalid QVariant if it cannot be read. type says what the resource is for; Nt reads the text of local files whatever it is. Reimplement it to show documents from elsewhere.

[override, protected] void QTextBrowser::mouseMoveEvent(QMouseEvent *event)

Reimplements QTextEdit::mouseMoveEvent().

Emits highlighted() when the mouse comes over a link or leaves it.

[override, protected] void QTextBrowser::mousePressEvent(QMouseEvent *event)

Reimplements QTextEdit::mousePressEvent().

[override, protected] void QTextBrowser::mouseReleaseEvent(QMouseEvent *event)

Reimplements QTextEdit::mouseReleaseEvent().

A click on a link emits anchorClicked(), and shows the target of the link if openLinks() is set.

Returns whether links to other programs are opened; recorded only.

Returns whether the browser shows the target of a link that is clicked. The default is true.

[slot, virtual] void QTextBrowser::reload()

Shows the current source again, read afresh.

QStringList QTextBrowser::searchPaths() const

Returns the directories that relative sources are looked up in.

Sets whether links to other programs are opened to open; recorded only.

Sets whether the browser shows the target of a link that is clicked to open. anchorClicked() is emitted in either case.

void QTextBrowser::setSearchPaths(const QStringList &paths)

Sets the directories that relative sources are looked up in to paths.

[slot, virtual] void QTextBrowser::setSource(const QUrl &name)

Shows the source name and adds it to the history, after the current source; the sources forward of it are forgotten. A source that cannot be read shows nothing.

See also source() and loadResource().

QUrl QTextBrowser::source() const

Returns the source that is shown, or an empty URL.

See also setSource().

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

This signal is emitted when the source changes to src.