引用webenginemo模块
在pro中添加QT += webenginewidgets
使用QWebEngineView
view = QWebEngineView(this);
view.load(QUrl("http://qt-project.org/"));
view.show();
加载网页
view.load(QUrl("http://qt-project.org/"));
或
view.setUrl(QUrl("http://qt-project.org/"));
加载HTML内容
view.setHtml("<html><body><p>hello world.</p></body></html>")
弹出窗口支持
如果允许用户打开新窗口的网站(如弹出窗口)提供支持,可以继承QWebEngineView并重新实现createWindow()函数。
QWebEngineView *WebEngineView::createWindow(QWebEnginePage::WebWindowType type)
{
//do something
return this;
}
信号处理
void loadStarted(); //页面加载时发出
void loadProgress(int progress); //页面加载进度
void loadFinished(bool ok); //页面加载结束,ok表示加载成功或失败
void iconChanged(const QIcon &icon); //网页图标改变时发出
void iconUrlChanged(const QUrl &url); //网页图标URL改变时发出
void selectionChanged(); //网页中选择内容改变时发出
void titleChanged(const QString &title);//网页标题改变时发出
void urlChanged(const QUrl &url); //加载的URL改变时发出
void renderProcessTerminated(QWebEnginePage::RenderProcessTerminationStatus terminationStatus, int exitCode);//进程非正常退出时发出,terminationStatus表示退出状态,exitCode表示退出状态码
主要方法说明
QWebEnginePage *page() const //函数返回指向网页对象的指针。 QWebEngineView包含一个QWebEnginePage,它允许访问页面上下文中的QWebEngineHistory。
QUrl iconUrl() const //获取网页图标url
QIcon icon() const //获取网页图标
QString title() const //获取网页标题
QUrl url() const //获取网页url
开启插件支持
//开启插件支持(如Flash player、文件拖拽等)
QWebEngineSettings::defaultSettings()->setAttribute(QWebEngineSettings::PluginsEnabled, true);
开启文件下载支持
QWebEngineView *web = new QWebEngineView(reinterpret_cast<QWebEngineView*>(this));
connect(web->page()->profile(), &QWebEngineProfile::downloadRequested, [this](QWebEngineDownloadItem *download) {
if (download->savePageFormat() != QWebEngineDownloadItem::UnknownSaveFormat)
{
}
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), download->path());//选择下载路径
if (fileName.isEmpty())
return;
download->setPath(fileName);//设置文件下载路径
qDebug() << download->path() << download->savePageFormat();
download->accept();//接收当前下载请求,只有接收后才会开始下载
});
网友评论