1. 程式人生 > >QWebEngineView如何忽略SSL證書錯誤

QWebEngineView如何忽略SSL證書錯誤

最近用QT寫客戶端軟體,思路是使用QWebEngineView來繪製本地的html或者伺服器上的html做介面展示。可是發現QWebEngineView在Load一個https的URL的時候,由於ssl證書不可信導致提示有錯誤,無法顯示內容,在QWebEngineView這個類裡面找了半天都沒看到忽略SSL證書錯誤的方法,後面終於找到了,原來在藏在QWebEnginePage這個類裡面。

這裡,需要你從QWebEnginePage繼承一個類,重寫certificateError方法

自定義的WebEnginePage類

#ifndef CUSTOMWEBENGINEPAGE_H
#define
CUSTOMWEBENGINEPAGE_H
#include <QWebEnginePage> class CustomWebEnginePage : public QWebEnginePage { public: CustomWebEnginePage(); virtual bool certificateError(const QWebEngineCertificateError &certificateError); }; #endif // CUSTOMWEBENGINEPAGE_H
#include "customwebenginepage.h"
CustomWebEnginePage::CustomWebEnginePage() { } bool CustomWebEnginePage::certificateError(const QWebEngineCertificateError &certificateError) { return true; }

然後在初始化QWebEngineView的時候,把這個自定義的WebEnginePage設定給View物件就好了。

_webView = new QWebEngineView(this);
_webView->setPage(new CustomWebEnginePage());

成功!