QT QWebEngineProcess使用注意事项
一、pro文件中需要添加模块
文章图片
二、程序打包 除了使用windeployqt.exe打包依赖库以外,还需要手动添加两项文件。
第一步:在你程序bin目录下的translations文件中添加文件夹qtwebengine_locales,并在里面粘贴QT 自带的qtwebengine_locales。
文章图片
QT 自带的在如下目录,将这些文件全部粘贴到你添加的那个文件夹中
文章图片
第二步:bin目录下添加文件夹resources,并放入5个文件
文章图片
5个文件在如下目录
文章图片
三、设置过滤器,拦截请求 【QT QWebEngineProcess使用注意事项】使用场景:扫码登录
扫码登录时,如果扫码成功,服务器会回调,返回一个成功登录的URL,URL中含token,就可以验证是否扫码,这样我们就可以直接通过设置过滤器来拦截到这个回调URL
添加过滤器代码
//.h文件
class WebUrlRequestInterceptor : public QWebEngineUrlRequestInterceptor
{
Q_OBJECTpublic:
WebUrlRequestInterceptor(QObject *p = Q_NULLPTR);
void interceptRequest(QWebEngineUrlRequestInfo &info);
signals:
void loginFinished();
};
//.cpp文件
WebUrlRequestInterceptor::WebUrlRequestInterceptor(QObject *p)
:QWebEngineUrlRequestInterceptor(p)
{}
void WebUrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) {
QString rsrct = "";
switch(info.resourceType()){
case 0:rsrct="ResourceTypeMainFrame = 0, // top level page";
break;
case 1:
{
rsrct="ResourceTypeSubFrame, // frame or iframe";
QString url = info.requestUrl().url();
if (url.contains("https://www.4399.com/api/login?token=")) {
qDebug()<<"登录成功"<
//WebViewForm.cpp文件中
WebUrlRequestInterceptor *ceptor = new WebUrlRequestInterceptor();
profile()->setRequestInterceptor(ceptor);
connect(ceptor, &WebUrlRequestInterceptor::loginFinished,
this, &WebViewForm::loginFinished);
推荐阅读
- 由浅入深理解AOP
- 【译】20个更有效地使用谷歌搜索的技巧
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus|MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决
- MybatisPlus使用queryWrapper如何实现复杂查询
- iOS中的Block
- Linux下面如何查看tomcat已经使用多少线程
- 使用composer自动加载类文件
- android|android studio中ndk的使用
- 使用协程爬取网页,计算网页数据大小