QWebView Class
QWebView 类提供了一个用于查看网页内容的窗口。更多
| Header: | #include <QWebView> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS WebView)target_link_libraries(mytarget PRIVATE Qt6::WebView) |
| qmake: | QT += webview |
| 自 | Qt 6.11 |
| 继承: | QWindow |
属性
|
公共功能
| bool | canGoBack() const |
| bool | canGoForward() const |
| QString | httpUserAgentString() const |
| bool | isLoading() const |
| int | loadProgress() const |
| QWebViewSettings * | settings() const |
| QString | title() const |
| QUrl | url() const |
公共插槽
| void | deleteAllCookies() |
| void | goBack() |
| void | goForward() |
| void | loadHtml(const QString &html, const QUrl &baseUrl = QUrl()) |
| void | reload() |
| void | setHttpUserAgentString(const QString &httpUserAgent) |
| void | setUrl(const QUrl &url) |
| void | stop() |
信号
| void | httpUserAgentStringChanged(const QString &userAgent) |
| void | loadProgressChanged(int loadProgress) |
| void | loadingChanged(const QWebViewLoadingInfo &loadingInfo) |
| void | titleChanged(const QString &title) |
| void | urlChanged(const QUrl &url) |
重新实现的受保护函数
| virtual bool | event(QEvent *e) override |
详细说明
网络视图是一种用于显示网络内容的窗口,在可用平台上使用本地 API 实现。因此,它并不一定需要将完整的网络浏览器栈作为应用程序的一部分。
可以使用setUrl() 函数将网站加载到网络视图中。
另外,如果有现成的 HTML 内容,也可以使用 setHtml() 代替。
loadingChanged() 信号会在视图开始加载和完全加载完毕时发出。其参数 QWebViewLoadRequest 对象表示加载成功还是失败。
HTML 文档的标题可通过title() 属性访问。
属性文档
httpUserAgentString : QString
该属性包含使用中的用户代理。用户代理字符串随 HTTP 发送,用于识别浏览器。
访问功能:
| QString | httpUserAgentString() const |
| void | setHttpUserAgentString(const QString &httpUserAgent) |
通知信号:
| void | httpUserAgentStringChanged(const QString &userAgent) |
[read-only] loadProgress : int
网页内容的当前加载进度,用 0 到 100 之间的整数表示。
访问功能:
| int | loadProgress() const |
通知信号:
| void | loadProgressChanged(int loadProgress) |
[read-only] loading : bool
如果WebView 当前正在加载新内容,则保存true ,否则保存false 。
访问功能:
| bool | isLoading() const |
通知信号:
| void | loadingChanged(const QWebViewLoadingInfo &loadingInfo) |
[read-only] settings : QWebViewSettings* const
QWebView 的设置对象。 网络视图的设置。
访问功能:
| QWebViewSettings * | settings() const |
另请参阅 WebViewSettings 。
title : QString
该属性包含当前加载的网页标题。
页面标题由 HTML<title> 元素定义。
访问功能:
| QString | title() const |
Notifier 信号:
| void | titleChanged(const QString &title) |
另请参阅 titleChanged().
url : QUrl
该属性包含当前查看的网页的 URL。
设置该属性可清除视图并加载 URL。默认情况下,该属性包含一个空的无效 URL。源于用户输入的 URL 应使用QUrl::fromUserInput() 进行解析。
注意: QWebView 不支持通过 Qt XML 资源系统加载内容。
访问功能:
| QUrl | url() const |
| void | setUrl(const QUrl &url) |
Notifier 信号:
| void | urlChanged(const QUrl &url) |
另请参阅 loadingChanged().
成员函数文档
bool QWebView::canGoBack() const
如果当前可以回溯网页历史记录,则返回true 。
bool QWebView::canGoForward() const
如果当前可以在网页历史记录中向前导航,则返回true 。
[slot] void QWebView::deleteAllCookies()
删除所有 cookie。
[override virtual protected] bool QWebView::event(QEvent *e)
重实现:QWindow::event(QEvent *ev)。
[slot] void QWebView::goBack()
回溯网页历史。
[slot] void QWebView::goForward()
在网页历史记录中向前导航。
[signal] void QWebView::httpUserAgentStringChanged(const QString &userAgent)
每当视图userAgent 发生变化时,就会发出该信号。
注: 用于httpUserAgentString 属性的通知信号。
另请参阅 httpUserAgent()。
[slot] void QWebView::loadHtml(const QString &html, const QUrl &baseUrl = QUrl())
将指定的html 内容载入网络视图。
该方法是url 属性的低级替代方法,后者通过 URL 引用 HTML 页面。
外部对象(如 HTML 文档中引用的样式表或图像)应相对于baseUrl 定位。例如,如果html 从http://www.example.com/documents/overview.html 获取,而 是基本 URL,那么使用相对 URLdiagram.png 引用的图片应位于http://www.example.com/documents/diagram.png 。
注意: QWebView 不支持通过 Qt XML 资源系统加载内容。
[signal] void QWebView::loadProgressChanged(int loadProgress)
该信号在网页加载过程中持续发出。loadProgress 参数是一个介于 0 和 100 之间的值,表示网页已加载的百分比。其预期用途是向用户显示进度条。
注: 有些后端不支持分段加载进度变化,只会在加载开始和结束时发出该信号,其值分别为 0 和 100。
注: 用于属性loadProgress 的通知信号。
另请参见 loadingChanged 。
[signal] void QWebView::loadingChanged(const QWebViewLoadingInfo &loadingInfo)
当网页内容的加载状态发生变化时,就会发出这个信号。例如,通过处理该信号,可以对页面加载错误做出反应。
loadingInfo 参数包含请求的url和状态,以及包含请求失败时错误信息的errorString。
注: 属性loading 的通知信号。
另请参阅 loadProgressChanged 和QWebViewLoadingInfo 。
[slot] void QWebView::reload()
重新读取当前url 。
[slot] void QWebView::stop()
停止加载当前url 。
[signal] void QWebView::titleChanged(const QString &title)
每当视图title 发生变化时,就会发出该信号。
注: 用于属性title 的通知信号。
另请参阅 title() 。
[signal] void QWebView::urlChanged(const QUrl &url)
每当视图url 发生变化时,就会发出该信号。
注: 用于属性url 的通知信号。
另请参阅 url() 。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.