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()
.
-
class PySide6.QtWidgets.QTextBrowser([parent=None])
- Parameters
parent – PySide6.QtWidgets.QWidget
Constructs an empty QTextBrowser
with parent parent
.
-
PySide6.QtWidgets.QTextBrowser.anchorClicked(arg__1)
- Parameters
arg__1 – PySide6.QtCore.QUrl
-
PySide6.QtWidgets.QTextBrowser.backward()
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.
-
PySide6.QtWidgets.QTextBrowser.backwardAvailable(arg__1)
- Parameters
arg__1 – bool
-
PySide6.QtWidgets.QTextBrowser.backwardHistoryCount()
- Return type
int
Returns the number of locations backward in the history.
-
PySide6.QtWidgets.QTextBrowser.clearHistory()
Clears the history of visited documents and disables the forward and backward navigation.
-
PySide6.QtWidgets.QTextBrowser.doSetSource(name[, type=QTextDocument.UnknownResource])
- Parameters
-
Attempts to load the document at the given url
with the specified type
.
setSource()
calls . In Qt 5, setSource
(const QUrl
&url) was virtual. In Qt 6, is virtual instead, so that it can be overridden in subclasses.
-
PySide6.QtWidgets.QTextBrowser.forward()
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.
-
PySide6.QtWidgets.QTextBrowser.forwardAvailable(arg__1)
- Parameters
arg__1 – bool
-
PySide6.QtWidgets.QTextBrowser.forwardHistoryCount()
- Return type
int
Returns the number of locations forward in the history.
-
PySide6.QtWidgets.QTextBrowser.highlighted(arg__1)
- Parameters
arg__1 – PySide6.QtCore.QUrl
-
PySide6.QtWidgets.QTextBrowser.historyChanged()
-
PySide6.QtWidgets.QTextBrowser.historyTitle(arg__1)
- Parameters
arg__1 – int
- Return type
str
Returns the documentTitle()
of the HistoryItem.
backaction.setToolTip(browser.historyTitle(-1))
forwardaction.setToolTip(browser.historyTitle(+1))
-
PySide6.QtWidgets.QTextBrowser.historyUrl(arg__1)
- Parameters
arg__1 – int
- Return type
PySide6.QtCore.QUrl
Returns the url of the HistoryItem.
-
PySide6.QtWidgets.QTextBrowser.home()
Changes the document displayed to be the first document from the history.
-
PySide6.QtWidgets.QTextBrowser.isBackwardAvailable()
- Return type
bool
Returns true
if the text browser can go backward in the document history using backward()
.
-
PySide6.QtWidgets.QTextBrowser.isForwardAvailable()
- Return type
bool
Returns true
if the text browser can go forward in the document history using forward()
.
-
PySide6.QtWidgets.QTextBrowser.openExternalLinks()
- Return type
bool
Specifies whether QTextBrowser
should automatically open links to external sources using openUrl()
instead of emitting the anchorClicked
signal. Links are considered external if their scheme is neither file or qrc.
The default value is false.
-
PySide6.QtWidgets.QTextBrowser.openLinks()
- Return type
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.
-
PySide6.QtWidgets.QTextBrowser.reload()
Reloads the current set source.
-
PySide6.QtWidgets.QTextBrowser.searchPaths()
- Return type
list of strings
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.
-
PySide6.QtWidgets.QTextBrowser.setOpenExternalLinks(open)
- Parameters
open – bool
Specifies whether QTextBrowser
should automatically open links to external sources using openUrl()
instead of emitting the anchorClicked
signal. Links are considered external if their scheme is neither file or qrc.
The default value is false.
-
PySide6.QtWidgets.QTextBrowser.setOpenLinks(open)
- Parameters
open – 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.
-
PySide6.QtWidgets.QTextBrowser.setSearchPaths(paths)
- Parameters
paths – list of strings
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.
-
PySide6.QtWidgets.QTextBrowser.setSource(name[, type=QTextDocument.UnknownResource])
- Parameters
-
Attempts to load the document at the given url
with the specified type
.
If type
is UnknownResource
(the default), the document type will be detected: that is, if the url ends with an extension of .md
, .mkd
or .markdown
, the document will be loaded via setMarkdown()
; otherwise it will be loaded via setHtml()
. This detection can be bypassed by specifying the type
explicitly.
-
PySide6.QtWidgets.QTextBrowser.source()
- Return type
PySide6.QtCore.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()
or 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.
-
PySide6.QtWidgets.QTextBrowser.sourceChanged(arg__1)
- Parameters
arg__1 – PySide6.QtCore.QUrl
-
PySide6.QtWidgets.QTextBrowser.sourceType()
- Return type
ResourceType
This property holds the type of the displayed document.
This is 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.