Qt's QWebView class allows to render some HTML content with QWebView::setHtml(const QString &html, const QUrl &baseUrl) and of course it tries to load external resources if the HTML contains some references like <script src="..."></script>.
How can I turn this feature off for security reasons? I took a look at the header file and found no according virtual method to override and no according method to set the desired behavior.
Any dirty workarounds are welcome!
I found two separate solutions for
QWebViewandQWebEngineView.QWebView
This solution works well for
QWebViewbut I did not test it forWebEngineView(v.i).First, derive a class from
QNetworkReplythat contains no data:Also derive a class from
QNetworkAccessManagerand make it block all requests by always returning an instance of the aboveEmptyNetworkReply:Finally, create a
QWebViewwith aQWebPagethat uses the aboveRestrictiveNetworkAccessManager:console output of the above example:
Btw, one might also want to disable JavaScript:
QWebEngineView
There is a solution that works with
QWebEngineView(but not withQWebViewas far as I can tell from the documentation). However, it is probably a good idea to migrate fromQWebViewtoQWebEngineView, anyways (although I experienced an insane performance drop withQWebEngineView).First, derive a class from
QWebEngineUrlRequestInterceptorand make it block all requests except the very first one:Then, make sure that the
QWebEngineViewuses that intercepter:console output of the HTML loaded in the above example:
Btw, one might also want to disable JavaScript: