如何在QWebEngineView中通过C++忽略和启用/禁用SSL证书错误?我发现QWebEnginePage有虚拟方法证书Error,但无法通过QWebEngineView访问?
我现在的解决方案是从QWebEnginePage派生,覆盖方法certificateError,并通过调用setPage将派生类的实例传递给QWebEngineView。如果有人感兴趣,这里有一个例子。
网页.h
#ifndef WEBPAGE_H
#define WEBPAGE_H
#include <QWebEnginePage>
class WebPage : public QWebEnginePage
{
Q_OBJECT
public:
explicit WebPage(QWidget *parent = 0);
protected:
bool certificateError(const QWebEngineCertificateError &error) override;
};
#endif // WEBPAGE_H
网页.cpp
#include "webpage.h"
WebPage::WebPage(QWidget *parent) :
QWebEnginePage(parent)
{
}
bool WebPage::certificateError(const QWebEngineCertificateError &error)
{
// TODO: conditional treatment
return error.isOverridable();
}
用法:
auto webView = new QWebEngineView(this);
auto webPage = new WebPage(this);
webView->setPage(webPage);
在QT 6中,此插槽已更改为插槽。您可以连接它并调用acceptCertificate:
connect(this, &WebPage::certificateError, this, &WebPage::onCertificateError);
void WebPage::onCertificateError(QWebEngineCertificateError
error) {
#if IGNORE_SSL_ERRORS qWarning() << "WebPage:: ignoring certificate error: " << error.description();
auto mutableError = const_cast<QWebEngineCertificateError&>(error);
mutableError.acceptCertificate();
#endif
qCritical() << "WebPage:: certificate error: " << error.description(); }