覆盖QtWebkit中的HTTP错误页面

时间:2012-02-14 15:37:08

标签: qt qtwebkit

我想在QWebView中显示错误页面。例如,如果服务器返回 404错误,则QWebView应显示其自定义 404错误而不是从服务器发送的错误

我使用QWebPage::extension来处理错误情况,它适用于网络错误。 但是,如果服务器返回错误404 并同时返回错误页面,则会显示此页面,并且永远不会调用QWebPage::extension方法。

有没有办法强制QtWebKit调用错误处理程序并覆盖服务器错误页面?

2 个答案:

答案 0 :(得分:4)

在跳过代码足够长的时间后,尝试各种各样的事情,以下似乎至少解决了我的情况:

MyWebPageSubclass::MyWebPageSubclass(...) {
    connect(networkAccessManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(handleNetworkFinished(QNetworkReply*)));
    // ...
}

void MyWebPageSubclass::handleNetworkFinished(QNetworkReply *reply) {
    bool ok = false;
    int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(&ok);

    if (ok && httpStatus / 100 != 1 && httpStatus / 100 != 2 && httpStatus / 100 != 3) {
        if (!supportsExtension(ErrorPageExtension)) {
            return;
        }

        ErrorPageExtensionOption option;
        option.domain = Http;
        option.error = httpStatus;
        option.url = reply->url();
        option.errorString = reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
        ErrorPageExtensionReturn output;
        if (!extension(ErrorPageExtension, &option, &output)) {
            return;
        }

        mainFrame()->setContent(output.content, output.contentType, output.baseUrl);
    }
}

答案 1 :(得分:2)

请注意,如果替换handleNetworkFinished()插槽中的mainFrame内容,如果页面元素(如外部图像或脚本)出现网络错误,您可能会看到错误页面而不是加载页面。

另一种选择是重新实现QWebPage :: extension()和QWebPage :: supportsExtension(),在这种情况下,QT Webkit将调用您的extension()实现页面错误并用您提供的页面替换页面内容,像:

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();
    // errReturn->contentType = "text/html"
    // errReturn->encoding = "UTF-8"; // these values are defaults
    return true;
  }
  return false;
}

bool WebPage::supportsExtension(Extension extension) const
{
  if(extension == QWebPage::ErrorPageExtension) {
    return true;
  }
  return QWebPage::supportsExtension(extension);
}