我一直在浏览QT文档,以找到一种在加载页面并发出请求后从QWebEngineView返回http状态代码的方法。我知道QWebEngineView不会与QNetWorkAccessManager交互,这将使完成此工作变得轻而易举。那么,还有其他方法可以使用QWebEngineView从请求中获取HTTP状态代码吗?我尝试了多种方法,包括RequestInterceptor。下面是我尝试使用RequestInterceptor的摘要,但是没有提供实际的HTTP状态代码,而只是给我恒定的值,这是我不想要的。
/*************requestinterceptor.h**************/
#ifndef REQUESTINTERCEPTOR_H
#define REQUESTINTERCEPTOR_H
#include <QWebEngineUrlRequestInterceptor>
#include <QWebEngineUrlRequestInfo>
#include <QDebug>
#include <QUrl>
class RequestInterceptor : public QWebEngineUrlRequestInterceptor
{
public:
explicit RequestInterceptor(QObject * parent = Q_NULLPTR);
virtual void interceptRequest(QWebEngineUrlRequestInfo & info) Q_DECL_OVERRIDE;
};
#endif // REQUESTINTERCEPTOR_H
/*************requestinterceptor.cpp***************/
#include "requestinterceptor.h"
RequestInterceptor::RequestInterceptor(QObject * parent)
:QWebEngineUrlRequestInterceptor(parent){}
void RequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo & info)
{
QString rsrct = "";
switch(info.resourceType())
{
case 1:rsrct="ResourceTypeSubFrame, --> frame or iframe";break;
case 2:rsrct="ResourceTypeStylesheet, --> a CSS stylesheet";break;
case 3:rsrct="ResourceTypeScript, --> an external script";break;
case 4:rsrct="ResourceTypeImage, --> an image (jpg/gif/png/etc)";break;
case 5:rsrct="ResourceTypeFontResource, --> a font";break;
case 6:rsrct="ResourceTypeSubResource, --> an other subresource.";break;
case 7:rsrct="ResourceTypeObject, --> an object (or embed) tag for a plugin,";break;
case 8:rsrct="ResourceTypeMedia, --> a media resource.";break;
case 9:rsrct="ResourceTypeWorker, --> the main resource of a dedicated worker.";break;
case 10:rsrct="ResourceTypeSharedWorker, --> the main resource of a shared worker.";break;
case 11:rsrct="ResourceTypePrefetch, --> an explicitly requested prefetch";break;
case 12:rsrct="ResourceTypeFavicon, --> a favicon";break;
case 13:rsrct="ResourceTypeXhr, --> a XMLHttpRequest";break;
case 14:rsrct="ResourceTypePing, --> a ping request for <a ping>";break;
case 15:rsrct="ResourceTypeServiceWorker, --> the main resource of a service worker.";break;
case 16:rsrct="ResourceTypeUnknown";break;
default : rsrct="Unknown type";break;
}
qDebug()<<"\t"<<Q_FUNC_INFO<<":\n\t\t" <<
"WebUrlRequestInterceptor::interceptRequest " <<info.requestMethod()
<<"\r\n "<<info.requestUrl()<<" "<<rsrct <<"\r\n";
}
/*************mainwindow.cpp***************/
const QUrl url("http://google.com");
view = new WebView();
view = new QWebEngineView();
interceptor = new RequestInterceptor(view);
profile = new QWebEngineProfile(view);
profile->setRequestInterceptor(interceptor);
page = new QWebEnginePage(profile,view);
view->setPage(page);
答案 0 :(得分:0)
经过数小时的搜索,我发现目前无法通过QWebEngine获取HTTP状态代码。加载完成事件也不会返回任何状态代码
然后我找到了解决方法
setting = webpage.settings()
setting.setAttribute(QWebEngineSettings.ErrorPageEnabled, False)