QWebEngineView加载url后直接点击链接没有反应要怎么解决
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QWebEngineView加载url后直接点击链接没有反应要怎么解决相关的知识,希望对你有一定的参考价值。
参考技术A 这个bug与createWindow函数好像没关系。 在QWebEngineView里面,用户点击网页的链接时,会触发信号: urlChanged(QUrl) 但是QWebEngineView不会自动去加载新的链接。 所以需要自己为urlChanged(QUrl)新建一个槽函数,自己手动加载参数里的新链接...QWebEngineView拦截Url请求设置
QWebEngineView拦截Url请求设置
QWebEngineUrlRequestInterceptor类提供了一个拦截URL请求抽象基类,可通过实现QWebEngineUrlRequestInterceptor::interceptRequest接口拦截并修改相应的UIL请求,以达到相应需求效果,比如设置“Accept-Language”。
class TWebEngineUrlRequestInterceptor : public QWebEngineUrlRequestInterceptor { Q_OBJECT public: TWebEngineUrlRequestInterceptor(QObject *parent = Q_NULLPTR); void interceptRequest(QWebEngineUrlRequestInfo &info); };
拦截接口实现
TWebEngineUrlRequestInterceptor::TWebEngineUrlRequestInterceptor(QObject *parent) : QWebEngineUrlRequestInterceptor(parent) { } void TWebEngineUrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) { QString strInfo = ""; switch (info.resourceType()) { case 0: //Top level page strInfo = "ResourceTypeMainFrame"; break; case 1: //Frame strInfo = "ResourceTypeSubFrame"; break; case 2: //CSS stylesheet strInfo = "ResourceTypeStylesheet"; break; case 3: //External script strInfo = "ResourceTypeScript"; break; case 4: //Image strInfo = "ResourceTypeImage"; break; case 5: //Font strInfo = "ResourceTypeFontResource"; break; case 6: //Sub-resource strInfo = "ResourceTypeSubResource"; break; case 7: //Plugin object strInfo = "ResourceTypeObject"; break; case 8: //Media resource strInfo = "ResourceTypeMedia"; break; case 9: //Resource of dedicated worker strInfo = "ResourceTypeWorker"; break; case 10: //Resource of shared worker strInfo = "ResourceTypeSharedWorker"; break; case 11: //Explicitly requested prefetch strInfo = "ResourceTypePrefetch"; break; case 12: //Favicon strInfo = "ResourceTypeFavicon"; break; case 13: //XML http request strInfo = "ResourceTypeXhr"; break; case 14: //Ping request strInfo = "ResourceTypePing"; break; case 15: //Resource of service worker strInfo = "ResourceTypeServiceWorker"; break; case 16: //Unknown resource strInfo = "ResourceTypeUnknown"; break; default: strInfo = "Unknown type"; break; } qDebug() << "RequestMethod===" << info.requestMethod() << "\n" << "RequestUrl===" << info.requestUrl() << "\n" << "RequestType===" << strInfo; //Set the value of the Accept-Language HTTP request-header. info.setHttpHeader("Accept-Language", "zh-CN"); }
接口调用,通过设置默认QWebEngineProfile属性
TWebEngineUrlRequestInterceptor *webInterceptor = new TWebEngineUrlRequestInterceptor(); QWebEngineProfile::defaultProfile()->setRequestInterceptor(webInterceptor);
自己的实际代码例子:
#include "cmywebengineurlrequestinterceptor.h" #include "QDebug" #include "../../vDownLoadUi/src/vIoDownfile_global.h" #include "../../common/common/common.h" #include "../../vIoMainUi/src/mainframe.h" extern MainFrame *g_pMainframe; Downfilewidget *g_DownFileDialog1 = NULL; extern Downfilewidget *g_DownFileDialog; CMyWebEngineUrlRequestInterceptor::CMyWebEngineUrlRequestInterceptor(QObject *parent) : QWebEngineUrlRequestInterceptor(parent) { } bool CMyWebEngineUrlRequestInterceptor::LoadDownfileUISO() { CreateObjectPtr CreateObjPtr = NULL; ReleaseObjectPtr ReleaseObjPtr = NULL; bool z_bRet = InitDllImport("vrvlib/libvDownLoadUi.so", CreateObjPtr, ReleaseObjPtr); if (!z_bRet || NULL == CreateObjPtr || NULL == ReleaseObjPtr) { QMessageBox msgBox; msgBox.setText("InitDllImport libvDownLoadUi.so failed."); msgBox.exec(); return false; } g_DownFileDialog1 = (Downfilewidget*)CreateObjPtr("DownLoadUi"); if (g_DownFileDialog1 == NULL) { QMessageBox msgBox; msgBox.setText("DownLoadUi new failed."); msgBox.exec(); return false; } return true; } bool g_test = false; void CMyWebEngineUrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) { // if (g_test == false) // { // g_test = true; // emit g_MainFrame->signal_ShowDownLoadUI("207830", 2); // //g_DownFileDialog->DownloadFile("207830", 2, 1, 1); // } QString strInfo = ""; switch (info.resourceType()) { case 0: //Top level page strInfo = "ResourceTypeMainFrame"; break; case 1: //Frame strInfo = "ResourceTypeSubFrame"; break; case 2: //CSS stylesheet strInfo = "ResourceTypeStylesheet"; break; case 3: //External script strInfo = "ResourceTypeScript"; break; case 4: //Image strInfo = "ResourceTypeImage"; break; case 5: //Font strInfo = "ResourceTypeFontResource"; break; case 6: //Sub-resource strInfo = "ResourceTypeSubResource"; break; case 7: //Plugin object strInfo = "ResourceTypeObject"; break; case 8: //Media resource strInfo = "ResourceTypeMedia"; break; case 9: //Resource of dedicated worker strInfo = "ResourceTypeWorker"; break; case 10: //Resource of shared worker strInfo = "ResourceTypeSharedWorker"; break; case 11: //Explicitly requested prefetch strInfo = "ResourceTypePrefetch"; break; case 12: //Favicon strInfo = "ResourceTypeFavicon"; break; case 13: //XML http request strInfo = "ResourceTypeXhr"; break; case 14: //Ping request strInfo = "ResourceTypePing"; break; case 15: //Resource of service worker strInfo = "ResourceTypeServiceWorker"; break; case 16: //Unknown resource strInfo = "ResourceTypeUnknown"; break; default: strInfo = "Unknown type"; break; } // qDebug()<< "RequestMethod===" << info.requestMethod() << "\n" // << "RequestUrl===" << info.requestUrl() << "\n" // << "RequestType===" << strInfo; QString z_TaskSerial = ""; int z_TaskType = 0; //?UserAccount=n2&TaskSerial=1234567&TaskType=1&DownAll=0 QUrl z_url(info.requestUrl()); QString z_strUrl = z_url.toString(); //QString z_strUrl = QString("downloadurl/downfile.html?UserAccount=n2&TaskSerial=1234567&TaskType=1&DownAll=0"); QString z_strDownloadurl = QString("downloadurl/downfile.html"); if(strstr(z_strUrl.toStdString().c_str(), z_strDownloadurl.toStdString().c_str())) { qDebug()<<"z_strUrl = "<<z_strUrl; QStringList z_StringList = z_strUrl.split(QRegExp("&")); QString z_Tmp = z_StringList.at(1); z_TaskSerial = z_Tmp.section(‘=‘, 1, 1); z_Tmp = z_StringList.at(2); z_TaskType = z_Tmp.section(‘=‘, 1, 1).toInt(); //LoadDownfileUISO(); if(g_DownFileDialog != NULL) { //g_DownFileDialog->DownloadFile(z_TaskSerial, z_TaskType, 1, 1); emit g_pMainframe->signal_ShowDownLoadUI(z_TaskSerial, z_TaskType); //g_DownFileDialog->DownloadFile("207830", 2, 1, 1); } } //Set the value of the Accept-Language HTTP request-header. // info.setHttpHeader("Accept-Language", "zh-CN"); // //Blocks this request if shouldBlock is true, so that it will not proceed. //This function can be used to prevent navigating away from a given domain, for example. // info.block(true); }
#ifndef CMYWEBENGINEURLREQUESTINTERCEPTOR_H #define CMYWEBENGINEURLREQUESTINTERCEPTOR_H #include <QWebEngineUrlRequestInterceptor> class CMyWebEngineUrlRequestInterceptor : public QWebEngineUrlRequestInterceptor { Q_OBJECT public: CMyWebEngineUrlRequestInterceptor(QObject *parent = Q_NULLPTR); void interceptRequest(QWebEngineUrlRequestInfo &info); bool LoadDownfileUISO(); }; #endif // CMYWEBENGINEURLREQUESTINTERCEPTOR_H
CMyWebEngineUrlRequestInterceptor *webInterceptor = new CMyWebEngineUrlRequestInterceptor(); QWebEngineProfile::defaultProfile()->setRequestInterceptor(webInterceptor);
以上是关于QWebEngineView加载url后直接点击链接没有反应要怎么解决的主要内容,如果未能解决你的问题,请参考以下文章
Qt编程使用QWebEngineView加载网页后,左键点击链接没反应,不能打开链接要怎么解决
如何将 swf 文件从内存加载到 QWebEngineView
如何在使用 QWebEngineView 'loadFinished' 加载页面后立即更改 html 元素?