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

2

6

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

Vicinage answered 26/2, 2011 at 10:17 Comment(4)
Are resources referenced in the html file local or remote?Sir
Sorry. I'm talking about web-resourcesVicinage
How do you load a file from resources into QWebView?Sir
@Piotr Dobrogost: I load it via the load() method.Vicinage
S
5

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();
}
Sir answered 26/2, 2011 at 13:15 Comment(2)
This is not exactly my Problem. I'm using load() by the way. But my problem is that I have a in my html file a script tag: <script src="external.org/script.js"></script>. Using that in local file context works perfectly but loaded from resources it simply isn't fetched and executed. I really can't understand that. Thanks for your helpVicinage
@Vicinage Right, my solution is only for relative resources if you don't want to put them in resource system.Sir
A
2

External URLs must have a schema to make them external, otherwise "external.org/script.js" looks for "script.js" under the "external.org/" sub-path, "http://external.org/script.js" is an absolute URL.

Edit:
Say you have this HTML file as the resource ":/file.html" and it is coppied from "http://example.com/":

<html>
 <head>
  <title>My HTML</title>
  <script type="text/javascript" src="/code.js"></scipt>
 </head>
 <body>
  <img href="/image.jpg" />
 </body>
</html>

Then to display this correctly you would need to do the following:

QFile res(":/file.html");
res.open(QIODevice::ReadOnly|QIODevice::Text);
my_webview.setHtml(res.readAll(), QUrl("http://example.com/");

That way, WebKit knows where to fetch "code.js" and "image.jpg" from. Using QWebView::load() will not work, as the root URL will be some internal URL, the one starting with qrc://, and WebKit will look for "code.js" and "image.jpg" in your applications resources. Basically, you can only use load() when all the relative URLs in the document come from the same place as the URL is pointing to. And if you used load(QUrl("qrc:///file.html")); in the case above, the URL (qrc:///file.html) is pointing to your resource system.

If you want to also include your resources in the HTML, you can use the qrc:// URLs in the HTML file.

Antonantone answered 26/2, 2011 at 15:43 Comment(1)
Sorry again. All my urls have a scheme :-) My mistakeVicinage

© 2022 - 2024 — McMap. All rights reserved.