3

I'm trying to load a local HTML file into a Qt 5.4.1 QtWebView:

import QtQuick 2.2
import QtQuick.Controls 1.1
import QtWebView 1.0

ApplicationWindow {
    visible: true
    title: webView.title

    WebView {
        id: webView
        anchors.fill: parent
        url: "qrc:/index.html"
    }
}

The HTML file is referenced in the qrc, and everything works as expected on the desktop.

However, when I deploy to Android, the webview fails to load the local file (although it works if I use a URL on the Web).

I could not find any hint in the documentation nor in the qt bug tracker (ie, as far as I understand, it is supposed to work).

skadge
  • 121
  • 7

2 Answers2

2

Alright, based on fparmana suggestion, the following code copies all resources in the qrc to a temporary local storage, that can then be loaded:

QString tmploc = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
QDir tmpdir(tmploc + "/my_little_project");

QDirIterator it(":", QDirIterator::Subdirectories);
while (it.hasNext()) {
    QString tmpfile;
    tmpfile = it.next();
    if (QFileInfo(tmpfile).isFile()) {
        QFileInfo file = QFileInfo(tmpdir.absolutePath() + tmpfile.right(tmpfile.size()-1));
        file.dir().mkpath("."); // create full path if necessary
        QFile::remove(file.absoluteFilePath()); // remove previous file to make sure we have the latest version
        QFile::copy(tmpfile, file.absoluteFilePath())
    }
}

// if wanted, set the QML webview URL
context->setContextProperty(QStringLiteral("baseUrl"), QFileInfo(tmpdir.absolutePath() + "/index.html").absoluteFilePath());
skadge
  • 121
  • 7
0

Based on this Load local HTML file into WebView try extract index.html to local storage directory first. and then change url to absolute path.

QString resourcesPath = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation).at(0);
resourcesPath.append(QDir::separator());
resourcesPath.append("index.html");
QFile::copy("qrc:/index.html", resourcesPath);

pass resourcesPath value to qml and change url webview with that value.

Not tested yet, but maybe it will work.

Community
  • 1
  • 1
fpermana
  • 86
  • 1