本页内容

QWebView Class

QWebView 类提供了一个用于查看网页内容的窗口。更多内容...

标题: #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
void runJavaScript(const QString &script, const std::function<void (const QVariant &)> &resultCallback = {})
QWebViewSettings *settings() const
QString title() const
QUrl url() const

公共插槽

void deleteAllCookies()
void deleteCookie(const QString &domain, const QString &name)
void goBack()
void goForward()
void loadHtml(const QString &html, const QUrl &baseUrl = QUrl())
void reload()
void setCookie(const QString &domain, const QString &name, const QString &value)
void setHttpUserAgentString(const QString &httpUserAgent)
void setUrl(const QUrl &url)
void stop()

信号

void cookieAdded(const QString &domain, const QString &name)
void cookieRemoved(const QString &domain, const QString &name)
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

详细说明

Web 视图是一个用于显示 Web 内容的窗口,在支持原生 API 的平台上,该视图是通过原生 API 实现的。因此,应用程序并不一定需要包含完整的 Web 浏览器栈。

可以使用setUrl() 函数将网站加载到 WebView 中。

此外,如果您已有现成的 HTML 内容,也可以使用 setHtml() 代替。

当视图开始加载以及完全加载完毕时,会发出loadingChanged() 信号。其参数 QWebViewLoadRequest 对象指示加载是成功还是失败。

可以通过title() 属性访问 HTML 文档的标题。

属性文档

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 的设置对象。WebView 的设置。

访问函数:

QWebViewSettings *settings() const

另请参阅 WebViewSettings 。

title : QString

该属性存储当前加载的网页的标题。

该页面标题由 HTML<title> 元素定义。

访问函数:

QString title() const

通知信号:

void titleChanged(const QString &title)

另请参阅 titleChanged()。

url : QUrl

该属性存储当前浏览的网页的 URL。

设置此属性将清空视图并加载该 URL。默认情况下,此属性包含一个空的、无效的 URL。来自用户输入的 URL 应使用 `QUrl::fromUserInput()` 进行解析。

注意: QWebView 不支持通过 Qt 资源系统加载内容。

访问函数:

QUrl url() const
void setUrl(const QUrl &url)

通知信号:

void urlChanged(const QUrl &url)

另请参阅 loadingChanged()。

成员函数文档

bool QWebView::canGoBack() const

如果当前可以在网页历史记录中返回,则返回true 。

bool QWebView::canGoForward() const

如果当前可以在网页历史记录中向前导航,则返回true 。

[signal] void QWebView::cookieAdded(const QString &domain, const QString &name)

当添加一个 Cookie 时,会发出此信号。

这些参数提供了关于已添加 Cookie 的domain 及name 的信息。

注意:当将 Qt WebEngine 模块用作后端时 ,凡是添加到底层QWebEngineCookieStore 中的Cookie(包括由网站添加的Cookie),都会触发cookieAdded信号。在其他情况下,cookieAdded信号仅在通过setCookie()显式添加Cookie时触发。

[signal] void QWebView::cookieRemoved(const QString &domain, const QString &name)

当一个 Cookie 被删除时,会触发此信号。这些参数提供了被删除的 Cookie 的domain 和name 的信息。

[slot] void QWebView::deleteAllCookies()

删除所有 Cookie。

[slot] void QWebView::deleteCookie(const QString &domain, const QString &name)

删除具有指定domain 和name 的Cookie。

当该Cookie被删除时,将触发cookieRemoved()信号。

[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 的Notifier 信号。

[slot] void QWebView::loadHtml(const QString &html, const QUrl &baseUrl = QUrl())

将指定的html 内容加载到Web视图中。

该方法提供了url 属性的低级替代方案,该属性通过URL引用HTML页面。

HTML 文档中引用的样式表或图像等外部对象应相对于 `baseUrl` 进行定位。例如,如果从基 URLhttp://www.example.com/documents/overview.html 获取html ,那么使用相对 URLdiagram.png 引用的图像应位于http://www.example.com/documents/diagram.png 。

注意: QWebView 不支持通过 Qt 资源系统加载内容。

[signal] void QWebView::loadProgressChanged(int loadProgress)

在网页加载过程中,该信号会持续发出。loadProgress 参数的取值范围为0到100,表示网页已加载的百分比。该信号的用途是向用户显示进度条。

注意:某些 后端不支持加载进度的小数变化,仅会在加载开始和结束时发出此信号,其值分别为 0 和 100。

注意: 属性 `loadProgress`的通知器 信号。

另请参阅 loadingChanged 。

[signal] void QWebView::loadingChanged(const QWebViewLoadingInfo &loadingInfo)

当网页内容的加载状态发生变化时,会发出此信号。通过处理此信号,可以实现对页面加载错误等情况的响应。

loadingInfo 参数包含请求的URL和状态,以及一个名为errorString 的字符串,其中包含请求失败时的错误信息。

注意: 这是针对属性 `loading` 的Notifier 信号。

另请参阅 loadProgressChanged 和QWebViewLoadingInfo 。

[slot] void QWebView::reload()

重新加载当前的url 。

void QWebView::runJavaScript(const QString &script, const std::function<void (const QVariant &)> &resultCallback = {})

执行指定的 JavaScript 代码。如果提供了 `resultCallback ` 函数,则在 `script ` 执行完毕后,该函数将被调用。

view.runJavaScript("document.title", [](const QVariant &v) { qDebug() << v.toString(); });

[slot] void QWebView::setCookie(const QString &domain, const QString &name, const QString &value)

添加一个包含指定domain 、name 和value 的Cookie。当Cookie被添加时,将触发cookieAdded()信号。

[slot] void QWebView::stop()

停止加载当前的url 。

[signal] void QWebView::titleChanged(const QString &title)

每当视图的title 发生变化时,都会发出此信号。

注意: 这是属性 `title`的通知器 信号。

另请参阅 title()。

[signal] void QWebView::urlChanged(const QUrl &url)

每当视图的url 发生变化时,都会发出此信号。

注意: 这是属性 `url` 的Notifier 信号。

另请参阅 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.