QWebEngineView Class
QWebEngineView 提供了一个widget, 用于查看和编辑web元素. 更多...
头文件: | #include <QWebEngineView> |
qmake: | QT += webenginewidgets |
开始支持版本: | Qt 5.4 |
基类: | QWidget |
属性
|
|
公有函数
QWebEngineView(QWidget *parent = Q_NULLPTR) | |
virtual | ~QWebEngineView() |
void | findText(const QString &subString, QWebEnginePage::FindFlags options = QWebEnginePage::FindFlags(), const QWebEngineCallback<bool> &resultCallback = QWebEngineCallback<bool>()) |
bool | hasSelection() const |
QWebEngineHistory * | history() const |
QIcon | icon() const |
QUrl | iconUrl() const |
void | load(const QUrl &url) |
void | load(const QWebEngineHttpRequest &request) |
QWebEnginePage * | page() const |
QAction * | pageAction(QWebEnginePage::WebAction action) const |
QString | selectedText() const |
void | setContent(const QByteArray &data, const QString &mimeType = QString(), const QUrl &baseUrl = QUrl()) |
void | setHtml(const QString &html, const QUrl &baseUrl = QUrl()) |
void | setPage(QWebEnginePage *page) |
void | setUrl(const QUrl &url) |
void | setZoomFactor(qreal factor) |
QWebEngineSettings * | settings() const |
QString | title() const |
void | triggerPageAction(QWebEnginePage::WebAction action, bool checked = false) |
QUrl | url() const |
qreal | zoomFactor() const |
重新实现的公有函数
virtual QSize | sizeHint() const override |
- 216 个公有函数继承自 QWidget
公有槽函数
- 19 个公有槽函数继承自 QWidget
信号
void | iconChanged(const QIcon &icon) |
void | iconUrlChanged(const QUrl &url) |
void | loadFinished(bool ok) |
void | loadProgress(int progress) |
void | loadStarted() |
void | renderProcessTerminated(QWebEnginePage::RenderProcessTerminationStatus terminationStatus, int exitCode) |
void | selectionChanged() |
void | titleChanged(const QString &title) |
void | urlChanged(const QUrl &url) |
受保护的函数
virtual QWebEngineView * | createWindow(QWebEnginePage::WebWindowType type) |
virtual bool | event(QEvent *ev) |
重新实现的受保护函数
virtual void | closeEvent(QCloseEvent *event) override |
virtual void | contextMenuEvent(QContextMenuEvent *event) override |
virtual void | dragEnterEvent(QDragEnterEvent *e) override |
virtual void | dragLeaveEvent(QDragLeaveEvent *e) override |
virtual void | dragMoveEvent(QDragMoveEvent *e) override |
virtual void | dropEvent(QDropEvent *e) override |
virtual bool | event(QEvent *ev) override |
virtual void | hideEvent(QHideEvent *event) override |
virtual void | showEvent(QShowEvent *event) override |
- 35 个受保护的函数继承自 QWidget
其他继承的成员
详细描述
一个 web view 是组成Qt WebEngine web 浏览模块的主要 widget 之一. 它可以被用于多种应用去展示来自 Internet 的 web 内容.
一个 web site 可以通过 load() 函数加载到 web 视图. 一般总是使用 Get 方法去加载 URLs.
像所有Qt的 widget 一样, 必须调用 show() 函数才能显示 Web 视图. 下面的代码段说明了这一点:
QWebEngineView *view = new QWebEngineView(parent); view->load(QUrl("http://qt-project.org/")); view->show();
或者, setUrl() 可以被使用去加载 web 元素. 如果你有可读的 HTML 内容, 你可以使用 setHtml() 代替.
当视图开始加载的时候, 发出 loadStarted() 信号, 当 web 视图的一个元素被完整加载的时候, 发出 loadProgress() 信号, 例如一个embedded 图像或一个脚本. 当视图被完整加载时, 发出 loadFinished() 信号. 其参数 true
或 false
指示加载是成功还是失败.
page() 函数返回一个指向 web page 对象的指针. 一个 QWebEngineView 包含一个 QWebEnginePage, 允许依次访问 QWebEngineHistory 在 web 页的内容.
HTML文档的标题可以用 title() 属性访问. 另外, 一个 web 元素可能是一个图标, 可以使用 icon() 访问, 或使用 iconUrl() 访问图标的 URL. 如果标题或是图标改变, 将发出 titleChanged(), iconChanged() 和 iconUrlChanged() 信号来回应. zoomFactor() 属性通过缩放因子, 能够缩放web页的内容.
这个类适合于上下文菜单, 并包含在浏览器中有用的操作. 对于自定义上下文菜单, 或将动作嵌入菜单或工具栏中, 可通过 pageAction() 获得单个动作. web 视图维护返回动作的状态, 但允许修改动作属性, 例如 text 或 icon. 动作语义也可以通过 triggerPageAction() 直接触发.
对于 web 元素, 如果你想支持用户去打开新的窗口, 例如弹出窗口, 你可以子类化 QWebEngineView, 并实现 createWindow() 函数.
另见 WebEngine Widgets Simple Browser Example, WebEngine Content Manipulation Example, WebEngine Markdown Editor Example.
Property Documentation
hasSelection : const bool
This property holds whether this page contains selected content or not.
By default, this property is false
.
Access functions:
bool | hasSelection() const |
See also selectionChanged().
icon : const QIcon
This property holds the icon associated with the page currently viewed.
By default, this property contains a null icon.
This property was introduced in Qt 5.7.
Access functions:
QIcon | icon() const |
Notifier signal:
void | iconChanged(const QIcon &icon) |
See also iconChanged(), iconUrl(), and iconUrlChanged().
iconUrl : const QUrl
This property holds the URL of the icon associated with the page currently viewed.
By default, this property contains an empty URL.
Access functions:
QUrl | iconUrl() const |
Notifier signal:
void | iconUrlChanged(const QUrl &url) |
See also iconUrlChanged(), icon(), and iconChanged().
selectedText : const QString
This property holds the text currently selected.
By default, this property contains an empty string.
Access functions:
QString | selectedText() const |
See also findText() and selectionChanged().
title : const QString
This property holds the title of the page as defined by the HTML <title>
element.
Equivalent to QWebEnginePage::title().
Access functions:
QString | title() const |
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.
Access functions:
QUrl | url() const |
void | setUrl(const QUrl &url) |
See also load() and urlChanged().
zoomFactor : qreal
This property holds the zoom factor for the view.
Valid values are within the range from 0.25
to 5.0
. The default factor is 1.0
.
Access functions:
qreal | zoomFactor() const |
void | setZoomFactor(qreal factor) |
Member Function Documentation
QWebEngineView::QWebEngineView(QWidget *parent = Q_NULLPTR)
Constructs an empty web view with the parent parent.
See also load().
[slot]
void QWebEngineView::back()
Convenience slot that loads the previous document in the list of documents built by navigating links. Does nothing if there is no previous document.
It is equivalent to:
view->page()->triggerAction(QWebEnginePage::Back);
See also forward() and pageAction().
[slot]
void QWebEngineView::forward()
Convenience slot that loads the next document in the list of documents built by navigating links. Does nothing if there is no next document.
It is equivalent to:
view->page()->triggerAction(QWebEnginePage::Forward);
See also back() and pageAction().
[signal]
void QWebEngineView::iconChanged(const QIcon &icon)
This signal is emitted when the icon ("favicon") associated with the view is changed. The new icon is specified by icon.
Note: Notifier signal for property icon.
This function was introduced in Qt 5.7.
See also icon(), iconUrl(), and iconUrlChanged().
[signal]
void QWebEngineView::iconUrlChanged(const QUrl &url)
This signal is emitted when the URL of the icon ("favicon") associated with the view is changed. The new URL is specified by url.
Note: Notifier signal for property iconUrl.
See also iconUrl(), icon(), and iconChanged().
[signal]
void QWebEngineView::loadFinished(bool ok)
This signal is emitted when a load of the page has finished. ok will indicate whether the load was successful or an error occurred.
See also loadStarted().
[signal]
void QWebEngineView::loadProgress(int progress)
This signal is emitted every time an element in the web view completes loading, such as an embedded image or a script. Therefore, it tracks the collective progress of loading the web view.
The current value is provided by progress and scales from 0 to 100, which is the default range of QProgressBar.
See also loadStarted() and loadFinished().
[signal]
void QWebEngineView::loadStarted()
This signal is emitted when a new load of the page is started.
See also loadProgress() and loadFinished().
[slot]
void QWebEngineView::reload()
Reloads the current document.
See also stop(), pageAction(), and loadStarted().
[signal]
void QWebEngineView::renderProcessTerminated(QWebEnginePage::RenderProcessTerminationStatus terminationStatus, int exitCode)
This signal is emitted when the render process is terminated with a non-zero exit status. terminationStatus is the termination status of the process and exitCode is the status code with which the process terminated.
This function was introduced in Qt 5.6.
[signal]
void QWebEngineView::selectionChanged()
This signal is emitted whenever the selection changes.
Note: When using the mouse to select text by left-clicking and dragging, the signal will be emitted for each new character selected, and not upon releasing the left mouse button.
See also selectedText().
[slot]
void QWebEngineView::stop()
Convenience slot that stops loading the document.
It is equivalent to:
view->page()->triggerAction(QWebEnginePage::Stop);
See also reload(), pageAction(), and loadFinished().
[signal]
void QWebEngineView::titleChanged(const QString &title)
This signal is emitted whenever the title of the view changes.
See also title().
[signal]
void QWebEngineView::urlChanged(const QUrl &url)
This signal is emitted when the url of the view changes.
[virtual]
QWebEngineView::~QWebEngineView()
Destroys the web view.
[override virtual protected]
void QWebEngineView::closeEvent(QCloseEvent *event)
Reimplements: QWidget::closeEvent(QCloseEvent *event).
[override virtual protected]
void QWebEngineView::contextMenuEvent(QContextMenuEvent *event)
Reimplements: QWidget::contextMenuEvent(QContextMenuEvent *event).
[virtual protected]
QWebEngineView *QWebEngineView::createWindow(QWebEnginePage::WebWindowType type)
This function is called from the createWindow() method of the associated QWebEnginePage each time the page wants to create a new window of the given type. For example, when a JavaScript request to open a document in a new window is issued.
Note: If the createWindow()
method of the associated page is reimplemented, this method is not called, unless explicitly done so in the reimplementation.
See also QWebEnginePage::createWindow().
[override virtual protected]
void QWebEngineView::dragEnterEvent(QDragEnterEvent *e)
Reimplements: QWidget::dragEnterEvent(QDragEnterEvent *event).
[override virtual protected]
void QWebEngineView::dragLeaveEvent(QDragLeaveEvent *e)
Reimplements: QWidget::dragLeaveEvent(QDragLeaveEvent *event).
[override virtual protected]
void QWebEngineView::dragMoveEvent(QDragMoveEvent *e)
Reimplements: QWidget::dragMoveEvent(QDragMoveEvent *event).
[override virtual protected]
void QWebEngineView::dropEvent(QDropEvent *e)
Reimplements: QWidget::dropEvent(QDropEvent *event).
[override virtual protected]
bool QWebEngineView::event(QEvent *ev)
Reimplements: QWidget::event(QEvent *event).
void QWebEngineView::findText(const QString &subString, QWebEnginePage::FindFlags options = QWebEnginePage::FindFlags(), const QWebEngineCallback<bool> &resultCallback = QWebEngineCallback<bool>())
Finds the specified string, subString, in the page, using the given options.
To clear the selection, just pass an empty string.
resultCallback must take a boolean parameter. It will be called with a value of true
if subString was found; otherwise the callback value will be false
.
Warning: We guarantee that the callback (resultCallback) is always called, but it might be done during page destruction. When QWebEnginePage is deleted, the callback is triggered with an invalid value and it is not safe to use the corresponding QWebEnginePage or QWebEngineView instance inside it.
See also selectedText() and selectionChanged().
[override virtual protected]
void QWebEngineView::hideEvent(QHideEvent *event)
Reimplements: QWidget::hideEvent(QHideEvent *event).
QWebEngineHistory *QWebEngineView::history() const
Returns a pointer to the view's history of navigated web pages.
It is equivalent to:
view->page()->history();
void QWebEngineView::load(const QUrl &url)
Loads the specified url and displays it.
Note: The view remains the same until enough data has arrived to display the new URL.
See also load(), setUrl(), url(), urlChanged(), and QUrl::fromUserInput().
void QWebEngineView::load(const QWebEngineHttpRequest &request)
Issues the specified request and loads the response.
This function was introduced in Qt 5.9.
See also load(), setUrl(), url(), urlChanged(), and QUrl::fromUserInput().
QWebEnginePage *QWebEngineView::page() const
Returns a pointer to the underlying web page.
See also setPage().
QAction *QWebEngineView::pageAction(QWebEnginePage::WebAction action) const
Returns a pointer to a QAction that encapsulates the specified web action action.
void QWebEngineView::setContent(const QByteArray &data, const QString &mimeType = QString(), const QUrl &baseUrl = QUrl())
Sets the content of the web view to data. If the mimeType argument is empty, it is assumed that the content is text/plain,charset=US-ASCII
.
External objects referenced in the content are located relative to baseUrl. For external objects to be loaded, baseUrl
cannot be empty.
The data is loaded immediately; external objects are loaded asynchronously.
See also load(), setHtml(), and QWebEnginePage::toHtml().
void QWebEngineView::setHtml(const QString &html, const QUrl &baseUrl = QUrl())
Sets the content of the web view to the specified html content.
External objects, such as stylesheets or images referenced in the HTML document, are located relative to baseUrl. For external objects to be loaded, baseUrl
cannot be empty. 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
.
The HTML document is loaded immediately, whereas external objects are loaded asynchronously.
When using this method, Qt WebEngine assumes that external resources, such as JavaScript programs or style sheets, are encoded in UTF-8 unless otherwise specified. For example, the encoding of an external script can be specified through the charset
attribute of the HTML script tag. Alternatively, the encoding can be specified by the web server.
This is a convenience function equivalent to setContent(html, "text/html;charset=UTF-8", baseUrl)
.
Warning: This function works only for HTML. For other MIME types (such as XHTML or SVG), setContent() should be used instead.
Note: Content larger than 2 MB cannot be displayed, because setHtml() converts the provided HTML to percent-encoding and places data
: in front of it to create the URL that it navigates to. Thereby, the provided code becomes a URL that exceeds the 2 MB limit set by Chromium. If the content is too large, the loadFinished() signal is triggered with success=false
.
See also load(), setContent(), QWebEnginePage::toHtml(), and QWebEnginePage::setContent().
void QWebEngineView::setPage(QWebEnginePage *page)
Makes page the new web page of the web view.
The parent QObject of the provided page remains the owner of the object. If the current page is a child of the web view, it will be deleted.
See also page().
QWebEngineSettings *QWebEngineView::settings() const
Returns a pointer to the view or page specific settings object.
It is equivalent to:
view->page()->settings();
See also QWebEngineSettings::defaultSettings().
[override virtual protected]
void QWebEngineView::showEvent(QShowEvent *event)
Reimplements: QWidget::showEvent(QShowEvent *event).
[override virtual]
QSize QWebEngineView::sizeHint() const
Reimplements an access function for property: QWidget::sizeHint.
void QWebEngineView::triggerPageAction(QWebEnginePage::WebAction action, bool checked = false)
Triggers the specified action. If it is a checkable action, the specified checked state is assumed.
The following example triggers the copy action and therefore copies any selected text to the clipboard.
view->triggerPageAction(QWebEnginePage::Copy);
See also pageAction().