QWebView Class | Qt WebView | Qt 6.12.0
Description: The QWebView class provides a window that is used to view web content.
Keywords: class

- Qt 6.12
- Qt WebView
- C++ Classes
- QWebView
- Qt 6.12.0 Reference Documentation

# QWebView Class

The QWebView class provides a window that is used to view web content. More...

Header: | `#include <QWebView>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS WebView)` `target_link_libraries(mytarget PRIVATE Qt6::WebView)`
qmake: | `QT += webview`
Since: | Qt 6.11
Inherits: | QWindow

- List of all members, including inherited members

## Properties

httpUserAgentString : QString loadProgress : int loading : bool | settings : QWebViewSettings* const title : QString url : QUrl

## Public Functions

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

## Public Slots

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

## Signals

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)

## Reimplemented Protected Functions

virtual bool | event(QEvent *e) override

## Detailed Description

A web view is a window for displaying web content that is implemented using native APIs on the platforms where they are available. Therefore, it does not necessarily require including a full web browser stack as part of the application.

A web site can be loaded to a web view with the setUrl() function.

Alternatively, if you have the HTML content readily available, you can use setHtml() instead.

The loadingChanged() signal is emitted when the view begins loading and when the view has been loaded completely. Its argument QWebViewLoadRequest object indicates whether loading was successful or failed.

The title of an HTML document can be accessed with the title() property.

## Property Documentation

### httpUserAgentString : QString

This property holds the user agent in use. The user-agent string sent with HTTP to identify the browser.

Access functions:

QString | httpUserAgentString() const
void | setHttpUserAgentString(const QString &httpUserAgent)

Notifier signal:

void | httpUserAgentStringChanged(const QString &userAgent)

### `[read-only]` loadProgress : int

The current load progress of the web content, represented as an integer between 0 and 100.

Access functions:

int | loadProgress() const

Notifier signal:

void | loadProgressChanged(int loadProgress)

### `[read-only]` loading : bool

Holds `true` if the WebView is currently in the process of loading new content, `false` otherwise.

Access functions:

bool | isLoading() const

Notifier signal:

void | loadingChanged(const QWebViewLoadingInfo &loadingInfo)

### `[read-only]` settings : QWebViewSettings* const

Settings object for the QWebView. The settings for a web view.

Access functions:

QWebViewSettings * | settings() const

See also WebViewSettings.

### title : QString

This property holds the title of the currently loaded web page.

The title of the page as defined by the HTML `<title>` element.

Access functions:

QString | title() const

Notifier signal:

void | titleChanged(const QString &title)

See also titleChanged().

### url : QUrl

This property holds the URL of the web page currently viewed.

Setting this property clears the view and loads the URL. By default, this property contains an empty, invalid URL. URLs that originate from user input should be parsed with QUrl::fromUserInput().

Note: QWebView does not support loading content through the Qt Resource system.

Access functions:

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

Notifier signal:

void | urlChanged(const QUrl &url)

See also loadingChanged().

## Member Function Documentation

### bool QWebView::canGoBack() const

Returns `true` if it's currently possible to navigate back in the web history.

### bool QWebView::canGoForward() const

Returns `true` if it's currently possible to navigate forward in the web history.

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

This signal is emitted when a cookie is added.

The parameters provide information about the domain and the name of the added cookie.

Note: When the Qt WebEngine module is used as a backend, the cookieAdded signal will be emitted for any cookie added to the underlying QWebEngineCookieStore, including those added by websites. In other cases cookieAdded signal is only emitted for cookies explicitly added with setCookie().

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

This signal is emitted when a cookie is deleted. The parameters provide information about the domain and the name of the deleted cookie.

### `[slot]` void QWebView::deleteAllCookies()

Deletes all the cookies.

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

Deletes a cookie with the specified domain and name.

The cookieRemoved() signal will be emitted when the cookie is deleted.

### `[override virtual protected]` bool QWebView::event(QEvent *e)

Reimplements: QWindow::event(QEvent *ev).

### `[slot]` void QWebView::goBack()

Navigates back in the web history.

### `[slot]` void QWebView::goForward()

Navigates forward in the web history.

### `[signal]` void QWebView::httpUserAgentStringChanged(const QString &userAgent)

This signal is emitted whenever the userAgent of the view changes.

Note: Notifier signal for property httpUserAgentString.

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

Loads the specified html content to the web view.

This method offers a lower-level alternative to the url property, which references HTML pages via URL.

External objects such as stylesheets or images referenced in the HTML document should be located relative to baseUrl. For example, if html is retrieved from `http://www.example.com/documents/overview.html`, which is the base URL, then an image referenced with the relative URL `diagram.png`, should be at `http://www.example.com/documents/diagram.png`.

Note: The QWebView does not support loading content through the Qt Resource system.

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

This signal is continuously emitted during the loading of a web page. The loadProgress parameter is a value between 0 and 100, indicating what percentage of the web page has been loaded. The intended use for this is to display a progress bar to the user.

Note: Some backends do not support fractional load progress changes, and will only emit this signal at the start and end of a load, with values of 0 and 100, respecively.

Note: Notifier signal for property loadProgress.

See also loadingChanged.

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

This signal is emitted when the state of loading the web content changes. By handling this signal it's possible, for example, to react to page load errors.

The loadingInfo parameter holds the url and status of the request, as well as an errorString containing an error message for a failed request.

Note: Notifier signal for property loading.

See also loadProgressChanged and QWebViewLoadingInfo.

### `[slot]` void QWebView::reload()

Reloads the current url.

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

Runs the specified JavaScript. In case a resultCallback function is provided, it will be invoked after the script finishes running.

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

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

Adds a cookie with the specified domain, name and value. The cookieAdded() signal will be emitted when the cookie is added.

### `[slot]` void QWebView::stop()

Stops loading the current url.

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

This signal is emitted whenever the title of the view changes.

Note: Notifier signal for property title.

See also title().

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

This signal is emitted whenever the url of the view changes.

Note: Notifier signal for property url.

See also url().