One note: if you replace the mainFrame content in the handleNetworkFinished () slot, you can see your error page instead of the loaded page if network errors occurred with page elements (for example, external images or scripts).
Another option would be to reimplement QWebPage :: extension () and QWebPage :: supports Extension (), in which case QT Webkit will call your implementation of extension () for page errors and replace the contents of the page with the page that you specified as:
class WebPage : public QWebPage { Q_OBJECT public: WebPage(QObject *parent = 0); bool extension(Extension extension, const ExtensionOption *option, ExtensionReturn * output); bool supportsExtension(Extension extension) const; }; bool WebPage::extension(Extension extension, const ExtensionOption *option, ExtensionReturn * output) { if(!option || !output) { return false; } if(extension == QWebPage::ErrorPageExtension) { const ErrorPageExtensionOption *errOption = static_cast<const ErrorPageExtensionOption*>(option); QString errPage; errPage = "<html><body><h1>"; errPage += "Page loading error, URL: "; errPage += errOption->url.toString(); errPage += "</h1><h3>Error occurred in the "; switch (errOption->domain) { case QWebPage::QtNetwork: errPage += "QT network layer, code is: "; break; case QWebPage::Http: errPage += "HTTP layer, HTTP error code is: "; break; case QWebPage::WebKit: errPage += "WebKit internals, error code is: "; break; default: errPage += "Unknown domain, error code is: "; } errPage += errOption->error; errPage += "</h3><h4><br>Error text: "; errPage += errOption->errorString; errPage += "</h4></body></html>"; ErrorPageExtensionReturn *errReturn = static_cast<ErrorPageExtensionReturn*>(output); errReturn->baseUrl = errOption->url; errReturn->content = errPage.toUtf8();
source share