QtWebKit在脚本块后停止解析

时间:2014-05-12 08:39:36

标签: c++ qt qt5 qtwebkit qwebpage

我遇到问题QWebFrame::setContent,看起来它无法从qt-project.org解析HTML,直到我从<header>删除了以下块(它只是停在那里而忽略了其余部分):

<script type="text/javascript">

function imgFitOnLoad(imgId)
{var maxWidth=720;var img=imgId;var width=img.clientWidth;if(width>maxWidth)
{var sized=maxWidth/width;var height=img.clientHeight*sized;img.width=maxWidth;img.height=height;}}

  var _gaq = _gaq || []; 
  _gaq.push(['_setAccount', 'UA-42656244-1']);
  _gaq.push (['_gat._anonymizeIp']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
  })();

</script>

我测试了其他几个网站(谷歌,维基百科,Youtube,GNU,......),但无法重现这个问题。最小(非)工作示例:

// MainWindow.cpp
#include "MainWindow.hpp"

#include <QDebug>
#include <QFile>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QWebPage>
#include <QWebFrame>
#include <QWebElementCollection>

MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent), mgr{}
{
    connect(&mgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(test(QNetworkReply*)));

    // unmodified -> assert fails
    mgr.get(QNetworkRequest{QUrl{"http://qt-project.org/"}});

    // modified -> assert passes
//    mgr.get(QNetworkRequest{QUrl{"https://gs93.de/tmp/qtprojectdump-mod.html"}});
}

void MainWindow::test(QNetworkReply* reply)
{
    qDebug() << __PRETTY_FUNCTION__;
    Q_ASSERT(reply->error() == QNetworkReply::NoError); // just to be sure


    auto data = reply->readAll();

    QFile file("orig.html");
    if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
        QTextStream stream(&file);
        stream << QString{data};
    }

    QWebPage page;
    page.mainFrame()->setContent(
        data,
        reply->header(QNetworkRequest::QNetworkRequest::ContentTypeHeader).toString());

    QFile file2("page.html");
    if (file2.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
        QTextStream stream(&file2);
        stream << page.mainFrame()->toHtml();
    }

    Q_ASSERT(page.mainFrame()->findAllElements("a").count() > 0); // unmodified version fails here
}

MainWindow::~MainWindow()
{
}

// MainWindow.hpp
#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP

#include <QWidget>
#include <QNetworkAccessManager>

class QNetworkReply;
class MainWindow : public QWidget
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    QNetworkAccessManager mgr;

public slots:
    void test(QNetworkReply* reply);
};

#endif // MAINWINDOW_HPP

编译
QT     += webkitwidgets
CONFIG += c++11

我正在使用Qt 5.2.1。这是我身边还是QtWebKit方面的错误?

0 个答案:

没有答案