QTextBrowser 類

QTextBrowser 類提供帶 Hypertext (超文本) 導航的富文本瀏覽器。 更多...

頭: #include <QTextBrowser>
qmake: QT += widgets
繼承: QTextEdit

特性

公共函數

QTextBrowser (QWidget * parent = nullptr)
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
QTextDocument::ResourceType sourceType () const

重實現公共函數

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

公共槽

virtual void backward ()
virtual void forward ()
virtual void home ()
virtual void reload ()
void setSource (const QUrl & url , QTextDocument::ResourceType type )
virtual void setSource (const QUrl & name )

信號

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

保護函數

void doSetSource (const QUrl & url , QTextDocument::ResourceType type = QTextDocument::UnknownResource)

重實現保護函數

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

詳細描述

此類延伸 QTextEdit (以隻讀模式),添加一些導航功能,以便用戶可以在超文本文檔中跟隨鏈接。

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 .

文檔源和內容

內容對於 QTextEdit are set with setHtml () 或 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.

若文檔名稱以錨點結尾 (例如 " #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 () 信號。

QTextBrowser provides backward () 和 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 使用 qrc:/docs/index.html as the URL with setSource ().

另請參閱 QTextEdit and QTextDocument .

特性文檔編製

modified : const bool

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

指定是否 QTextBrowser should automatically open links to external sources using QDesktopServices::openUrl () 而不是發射 anchorClicked signal. Links are considered external if their scheme is neither file or qrc.

默認值為 false。

該特性在 Qt 4.2 引入。

訪問函數:

bool openExternalLinks () const
void setOpenExternalLinks (bool open )

此特性指定是否 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.

默認值為 true。

該特性在 Qt 4.3 引入。

訪問函數:

bool openLinks () const
void setOpenLinks (bool open )

readOnly : const bool

This property holds whether the text browser is read-only

默認情況下,此特性為 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.

訪問函數:

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

默認情況下,此特性包含空 URL。

訪問函數:

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

sourceType : const QTextDocument::ResourceType

This property holds the type of the displayed document

這為 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 () 被調用。

訪問函數:

QTextDocument::ResourceType sourceType () const

undoRedoEnabled : const bool

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

默認情況下,此特性為 false .

成員函數文檔編製

QTextBrowser:: QTextBrowser ( QWidget * parent = nullptr)

Constructs an empty QTextBrowser with parent parent .

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

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.

[virtual slot] void 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.

另請參閱 forward () 和 backwardAvailable ().

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

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

[virtual slot] void 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.

另請參閱 backward () 和 forwardAvailable ().

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

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

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

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 .

注意: 信號 highlighted 在此類中被重載。通過使用函數指針句法連接到此信號,Qt 提供用於獲得如此範例展示的函數指針的方便幫助程序:

connect(textBrowser, QOverload<const QUrl &>::of(&QTextBrowser::highlighted),
    [=](const QUrl &link){ /* ... */ });
					

[signal] void QTextBrowser:: historyChanged ()

This signal is emitted when the history changes.

該函數在 Qt 4.4 引入。

另請參閱 historyTitle () 和 historyUrl ().

[virtual slot] void QTextBrowser:: home ()

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

[virtual slot] void QTextBrowser:: reload ()

Reloads the current set source.

[slot] void QTextBrowser:: setSource (const QUrl & url , QTextDocument::ResourceType type )

Attempts to load the document at the given url 采用指定 type .

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 QTextDocument::setMarkdown (); otherwise it will be loaded via QTextDocument::setHtml (). This detection can be bypassed by specifying the type explicitly.

注意: setter 函數對於特性 source .

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

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

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

int QTextBrowser:: backwardHistoryCount () const

Returns the number of locations backward in the history.

該函數在 Qt 4.4 引入。

void QTextBrowser:: clearHistory ()

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

該函數在 Qt 4.2 引入。

另請參閱 backward () 和 forward ().

[protected] void QTextBrowser:: doSetSource (const QUrl & url , QTextDocument::ResourceType type = QTextDocument::UnknownResource)

Attempts to load the document at the given url 采用指定 type .

setSource () calls doSetSource. In Qt 5, setSource (const QUrl &url) was virtual. In Qt 6, doSetSource() is virtual instead, so that it can be overridden in subclasses.

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

重實現: QAbstractScrollArea::event (QEvent *event).

[override virtual protected] bool QTextBrowser:: focusNextPrevChild ( bool next )

重實現: QTextEdit::focusNextPrevChild (bool next).

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

重實現: QTextEdit::focusOutEvent (QFocusEvent *e).

int QTextBrowser:: forwardHistoryCount () const

Returns the number of locations forward in the history.

該函數在 Qt 4.4 引入。

QString QTextBrowser:: historyTitle ( int i ) const

返迴 documentTitle () of the HistoryItem.

輸入 返迴
i < 0 backward () history
i == 0 current, see QTextBrowser::source ()
i > 0 forward () history
backaction.setToolTip(browser.historyTitle(-1));
forwardaction.setToolTip(browser.historyTitle(+1));
					

該函數在 Qt 4.4 引入。

QUrl QTextBrowser:: historyUrl ( int i ) const

Returns the url of the HistoryItem.

輸入 返迴
i < 0 backward () history
i == 0 current, see QTextBrowser::source ()
i > 0 forward () history

該函數在 Qt 4.4 引入。

bool QTextBrowser:: isBackwardAvailable () const

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

該函數在 Qt 4.2 引入。

另請參閱 backwardAvailable () 和 backward ().

bool QTextBrowser:: isForwardAvailable () const

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

該函數在 Qt 4.2 引入。

另請參閱 forwardAvailable () 和 forward ().

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

重實現: QTextEdit::keyPressEvent (QKeyEvent *e).

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

按鍵 動作
Alt+Left Arrow backward ()
Alt+Right Arrow forward ()
Alt+Up Arrow home ()

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

重實現: QTextEdit::loadResource (int type, const QUrl &name).

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
QTextDocument::MarkdownResource QString or QByteArray

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

重實現: QTextEdit::mouseMoveEvent (QMouseEvent *e).

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

重實現: QTextEdit::mousePressEvent (QMouseEvent *e).

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

重實現: QTextEdit::mouseReleaseEvent (QMouseEvent *e).

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

重實現: QTextEdit::paintEvent (QPaintEvent *event).