我试图将本地HTML文件加载到QT5.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"
}
}HTML文件在qrc中被引用,并且在桌面上一切正常工作。
但是,当我部署到Android时,webview无法加载本地文件(尽管如果我在Web上使用URL,它可以工作)。
我在文档和qt bug跟踪器中找不到任何提示(即,据我所知,它应该能工作)。
发布于 2015-03-16 10:29:36
好的,根据fparmana建议,以下代码将qrc中的所有资源复制到临时本地存储中,然后可以加载:
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());发布于 2015-03-13 03:42:27
基于这个Load local HTML file into WebView,首先尝试将index.html提取到本地存储目录。然后将url更改为绝对路径。
QString resourcesPath = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation).at(0);
resourcesPath.append(QDir::separator());
resourcesPath.append("index.html");
QFile::copy("qrc:/index.html", resourcesPath);将resourcesPath值传递给qml并使用该值更改url。
还没有测试,但也许它会起作用。
https://stackoverflow.com/questions/29020930
复制相似问题