QTextBrowser Class
The QTextBrowser class shows documents that the user can go back and forward through. More…
| Header | #include <QTextBrowser> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QTextEdit |
List of all members, including inherited members
Public Functions
| QTextBrowser(QWidget *parent = nullptr) | |
| ~QTextBrowser() | |
| QUrl | source() const |
| QStringList | searchPaths() const |
| void | setSearchPaths(const QStringList &paths) |
| virtual QVariant | loadResource(int type, const QUrl &name) |
| bool | isBackwardAvailable() const |
| bool | isForwardAvailable() const |
| void | clearHistory() |
| QString | historyTitle(int i) const |
| QUrl | historyUrl(int i) const |
| int | backwardHistoryCount() const |
| int | forwardHistoryCount() const |
| bool | openExternalLinks() const |
| void | setOpenExternalLinks(bool open) |
| bool | openLinks() const |
| void | setOpenLinks(bool open) |
Public Slots
| virtual void | setSource(const QUrl &name) |
| virtual void | backward() |
| virtual void | forward() |
| virtual void | home() |
| virtual void | reload() |
Signals
| void | backwardAvailable(bool available) |
| void | forwardAvailable(bool available) |
| void | historyChanged() |
| void | sourceChanged(const QUrl &src) |
| void | anchorClicked(const QUrl &link) |
| void | highlighted(const QUrl &link) |
Reimplemented Protected Functions
| void | keyPressEvent(QKeyEvent *event) |
| void | mousePressEvent(QMouseEvent *event) |
| void | mouseMoveEvent(QMouseEvent *event) |
| void | mouseReleaseEvent(QMouseEvent *event) |
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
QTextBrowser::QTextBrowser(QWidget *parent = nullptr)
Constructs an empty, read-only text browser with the given parent.
QTextBrowser::~QTextBrowser()
Destroys the text browser.
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.
void QTextBrowser::backward()
Goes back to the source before the current one.
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.
void QTextBrowser::forward()
Goes forward to the source after the current one.
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.
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.
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.
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.
void QTextBrowser::keyPressEvent(QKeyEvent *event)
Reimplements QTextEdit::keyPressEvent().
Alt+Left goes backward and Alt+Right forward.
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.
void QTextBrowser::mouseMoveEvent(QMouseEvent *event)
Reimplements QTextEdit::mouseMoveEvent().
Emits highlighted() when the mouse comes over a link or leaves it.
void QTextBrowser::mousePressEvent(QMouseEvent *event)
Reimplements QTextEdit::mousePressEvent().
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.
bool QTextBrowser::openExternalLinks() const
Returns whether links to other programs are opened; recorded only.
bool QTextBrowser::openLinks() const
Returns whether the browser shows the target of a link that is clicked. The default is true.
void QTextBrowser::reload()
Shows the current source again, read afresh.
QStringList QTextBrowser::searchPaths() const
Returns the directories that relative sources are looked up in.
void QTextBrowser::setOpenExternalLinks(bool open)
Sets whether links to other programs are opened to open; recorded only.
void QTextBrowser::setOpenLinks(bool open)
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.
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().
void QTextBrowser::sourceChanged(const QUrl &src)
This signal is emitted when the source changes to src.