QWebView doesn't load any external resources if it loads a html-file from qresources

domachine picture domachine · Feb 26, 2011 · Viewed 8.3k times · Source

As described in the title my problem is that qwebview doesn't load a html file correctly if it resides in my resources. It loads it perfectly if I load it from outside of the resources as normal local file. But this is not an option for me. I would like to bundle the file with the application.

EDIT: By the way, I'm talkin' about external resources from the web. (e.g. http://host.org/somejavascript.js) Thanks for any help

Answer

Piotr Dobrogost picture Piotr Dobrogost · Feb 26, 2011

Please take a look at the second parameter of
void QWebView::setHtml ( const QString & html, const QUrl & baseUrl = QUrl() ) According to documentation:

External objects such as stylesheets or images referenced in the HTML document are located relative to baseUrl.

Below is code that works for me.

#include <QtCore/QFile>
#include <QtCore/QUrl>
#include <QtGui/QApplication>
#include <QtGui/QMainWindow>
#include <QtWebKit/QWebView>


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QMainWindow window;
    QWebView webview(&window);

    QFile source(":/google.com.html");
    source.open(QIODevice::ReadOnly);
    webview.setHtml(QString::fromUtf8(source.readAll().constData()), QUrl("http://google.com"));
    window.setCentralWidget(&webview);
    window.show();

    return app.exec();
}