4

I'm having problems with QWebFrame::setContent, it looks like it fails to parse the HTML from qt-project.org until I remove the following block from <header> (it just stops there and ignores the rest):

<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>

I tested a few other sites (Google, Wikipedia, Youtube, GNU, ...) but couldn't reproduce the issue. Minimal (not) working example:

// 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()
{
}

and

// 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

compiled with

QT     += webkitwidgets
CONFIG += c++11

I'm using Qt 5.2.1. Is this a bug on my side or on the QtWebKit side?

gs93
  • 127
  • 3
  • 11
  • Have you tried the fancy browser example or the tabbed browser example? I also helped port a big Qt web browser project called QtWeb, that handles almost any web page pretty well. – phyatt Jun 27 '14 at 17:33
  • Unless you post code that actually compiles and illustrates the issue without all the extra cruft (i.e. extra files being loaded, pointless forward decls, etc.) I don't think you'll get much help with this one. – MrEricSir Jul 24 '14 at 02:45

0 Answers0