Qt WebEngine_hitzsf的博客-CSDN博客
一、QWebEngineView
QWebEngineView 类是一个实现Web浏览器的便捷类,提供了back() 、forward()、reload()、stop() 等方法,可轻松实现页面的前进、后退、重载等导航功能,要实现一个简单的只有网页加载网页预览、没有导航功能的web浏览器,只需要定义一个 QWebEngineView 类对象,使用load方法加载即可。下面是一个简单浏览器的实现
1、Widget类定义(widget.h)
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QUrl>
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr, QUrl url = QUrl("http://www.baidu.com"));
signals:
};
#endif // WIDGET_H
2、Widget类实现(widget.cpp)
#include "widget.h"
#include <QtWebEngineWidgets>
#include "mywebengineview.h"
Widget::Widget(QWidget *parent, QUrl url)
: QWidget(parent)
{
MyWebEngineView *view = new MyWebEngineView;
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(view);
mainLayout->setContentsMargins(0,0,0,0);
mainLayout->setMargin(0);
this->setContentsMargins(0,0,0,0);
this->setWindowFlags(Qt::FramelessWindowHint); // 去掉标题栏
view->load(url);
}
3、MyWebEngineView类定义(mywebengineview.h)
#ifndef MYWEBENGINEVIEW_H
#define MYWEBENGINEVIEW_H
#include <QtWebEngineWidgets>
class MyWebEngineView : public QWebEngineView
{
Q_OBJECT
public:
explicit MyWebEngineView(QWidget *parent = nullptr);
QWebEngineView * createWindow(QWebEnginePage::WebWindowType type) override;
void linkHovered(QString url);
void lessheadPagePrintfToPdf(bool ok);
private:
QUrl newUrl;
QWebEnginePage lessHeadPage;
};
#endif // MYWEBENGINEVIEW_H
4、MyWebEngineView类实现(mywebengineview.cpp)
#include "mywebengineview.h"
#include <QDebug>
MyWebEngineView::MyWebEngineView(QWidget *parent) : QWebEngineView(parent)
{
connect(this->page(),&QWebEnginePage::linkHovered,this,&MyWebEngineView::linkHovered);
connect(&lessHeadPage,&QWebEnginePage::loadFinished,this,&MyWebEngineView::lessheadPagePrintfToPdf);
}
//这个函数应该是由底层的QWebEnginePage发起调用的,如果不想新建QWebEngineView,在这儿覆写。
QWebEngineView *MyWebEngineView::createWindow(QWebEnginePage::WebWindowType type)
{
Q_UNUSED(type)
//识别要无头浏览输出pdf
if (newUrl.toString() == "https://www.baidu.com/s?wd=小米汽车&sa=fyb_n_homepage&rsv_dl=fyb_n_homepage&from=super&cl=3&tn=baidutop10&fr=top1000&rsv_idx=2&hisfilter=1" ||
newUrl.toString() == "https://www.baidu.com/s?wd=国风浩荡+文脉赓续&sa=fyb_n_homepage&rsv_dl=fyb_n_homepage&from=super&cl=3&tn=baidutop10&fr=top1000&rsv_idx=2&hisfilter=1")
{
lessHeadPage.load(newUrl);
}
else
{
this->load(newUrl);
}
qDebug() << "createWindow url is :" <<this->url();
return 0;
}
void MyWebEngineView::linkHovered(QString url)
{
qDebug() << "linkHovered url is :" <<url;
newUrl = QUrl(url);
}
//实现无头浏览输出pdf
void MyWebEngineView::lessheadPagePrintfToPdf(bool ok)
{
Q_UNUSED(ok)
lessHeadPage.printToPdf(QString("test.pdf"));
}
5、主函数(main.cpp)
#include <QApplication>
#include <QWebEngineProfile>
#include <QWebEngineSettings>
#include <QWebEnginePage>
#include <QWebEngineView>
#include <QtWebEngineWidgets>
#include "mywebengineview.h"
#include "widget.h"
QUrl commandLineUrlArgument()
{
const QStringList args = QCoreApplication::arguments();
for (const QString &arg : args.mid(1)) {
if (!arg.startsWith(QLatin1Char('-')))
return QUrl::fromUserInput(arg);
}
//return QUrl(QStringLiteral("http://www.wanweitech.cn"));
return QUrl(QStringLiteral("http://192.168.9.26:3006/#/Alautomdix"));
//return QUrl(QStringLiteral("http://www.baidu.com"));
}
int main(int argc, char *argv[])
{
QCoreApplication::setOrganizationName("qtweb_widget");
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
QApplication app(argc, argv);
QWebEngineSettings::defaultSettings()->setAttribute(QWebEngineSettings::PluginsEnabled, true); //启用加载插件
QWebEngineSettings::defaultSettings()->setAttribute(QWebEngineSettings::PdfViewerEnabled, true); //启用PDF查看
#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
QWebEngineSettings::defaultSettings()->setAttribute(QWebEngineSettings::DnsPrefetchEnabled, true);
QWebEngineProfile::defaultProfile()->setUseForGlobalCertificateVerification();
#endif
QUrl url = commandLineUrlArgument();
Widget view(nullptr, url);
view.showFullScreen(); //全屏显示
return app.exec();
}
二、无头浏览器
无头浏览器(Headless Browser)是一种没有图形用户界面(GUI)的浏览器。它通过在内存中渲染页面,然后将结果发送回请求它的用户或程序来实现对网页的访问,而不会在屏幕上显示网页。这种方式使得无头浏览器不仅适用于网络爬虫和测试等自动化任务,而且还能够更安全地进行网页浏览,因为它不会在屏幕上显示您的活动。
常见的无头浏览器包括Chrome Headless、PhantomJS、Puppeteer等,常见的这些无头浏览器都属于互联网前后端开发相关的东西,与Qt技术栈相关的比较少
1、基于QWebEnginePage实现无头浏览器
QWebEngine有两种实现方式,一个QWidget框架下的QWebEngineView,另一种是QML框架下的WebEngineView,其中QWebEngineView包含QWebEnginePage的功能,QWebEnginePage提供页面加载相关的功能,QWebEngineView用于page的显示,基于QWebEnginePage就可实现无头浏览器。
auto page = new QWebEnginePage(this);
page->setUrl(QUrl("https://www.qt.io/"));
connect(page, &QWebEnginePage::loadFinished, this, [=](bool ok){
qDebug() << __FUNCTION__ << "song" << "load is ok" << ok;
});
使用QWebEnginePage加载页面,页面加载成功后就可以做一些操作,如网络截图、爬虫、数据采集等。
QWebEnginePage支持QWebEngine内嵌浏览器相关的功能,具体可查看相关接口
1、获取网页相关信息
2、注入javascript
3、打印pdf
QML的WebEngineView没有像QWebEngineView把page的功能分开,所以WebEngineView不能实现无头浏览器的效果。
最后,需要注意的是,如果是在控制台程序中运行QWebEnginePage,main函数的应用程序类型必须是QApplication,使用QCoreApplication无法启动QWebEnginePage。
2、示例程序
mywebengineview.c
#include "mywebengineview.h"
#include <QDebug>
MyWebEngineView::MyWebEngineView(QWidget *parent) : QWebEngineView(parent)
{
connect(this->page(),&QWebEnginePage::linkHovered,this,&MyWebEngineView::linkHovered);
connect(&lessHeadPage,&QWebEnginePage::loadFinished,this,&MyWebEngineView::lessheadPagePrintfToPdf);
}
//这个函数应该是由底层的QWebEnginePage发起调用的,如果不想新建QWebEngineView,在这儿覆写。
QWebEngineView *MyWebEngineView::createWindow(QWebEnginePage::WebWindowType type)
{
Q_UNUSED(type)
//识别要无头浏览输出pdf
if (newUrl.toString() == "https://www.baidu.com/s?wd=小米汽车&sa=fyb_n_homepage&rsv_dl=fyb_n_homepage&from=super&cl=3&tn=baidutop10&fr=top1000&rsv_idx=2&hisfilter=1" ||
newUrl.toString() == "https://www.baidu.com/s?wd=国风浩荡+文脉赓续&sa=fyb_n_homepage&rsv_dl=fyb_n_homepage&from=super&cl=3&tn=baidutop10&fr=top1000&rsv_idx=2&hisfilter=1")
{
lessHeadPage.load(newUrl);
}
else
{
this->load(newUrl);
}
qDebug() << "createWindow url is :" <<this->url();
return 0;
}
void MyWebEngineView::linkHovered(QString url)
{
qDebug() << "linkHovered url is :" <<url;
newUrl = QUrl(url);
}
//实现无头浏览输出pdf
void MyWebEngineView::lessheadPagePrintfToPdf(bool ok)
{
Q_UNUSED(ok)
lessHeadPage.printToPdf(QString("test.pdf"));
}
mywebengineview.h
#ifndef MYWEBENGINEVIEW_H
#define MYWEBENGINEVIEW_H
#include <QtWebEngineWidgets>
class MyWebEngineView : public QWebEngineView
{
Q_OBJECT
public:
explicit MyWebEngineView(QWidget *parent = nullptr);
QWebEngineView * createWindow(QWebEnginePage::WebWindowType type) override;
void linkHovered(QString url);
void lessheadPagePrintfToPdf(bool ok);
private:
QUrl newUrl;
QWebEnginePage lessHeadPage;
};
#endif // MYWEBENGINEVIEW_H