qt 5.4 qtwebview:无法从 android 上的 qrc 加载本地 HTML
qt 5.4 qtwebview: failing to load local HTML from qrc on android
我正在尝试将本地 HTML 文件加载到 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"
}
}
qrc 中引用了 HTML 文件,在桌面上一切正常。
但是,当我部署到 Android 时,webview 无法加载本地文件(尽管如果我在 Web 上使用 URL 就可以)。
我在文档和 qt 错误跟踪器中都找不到任何提示(即,据我所知,它应该可以工作)。
基于此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 webview。
尚未测试,但也许会奏效。
好的,根据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());
我正在尝试将本地 HTML 文件加载到 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"
}
}
qrc 中引用了 HTML 文件,在桌面上一切正常。
但是,当我部署到 Android 时,webview 无法加载本地文件(尽管如果我在 Web 上使用 URL 就可以)。
我在文档和 qt 错误跟踪器中都找不到任何提示(即,据我所知,它应该可以工作)。
基于此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 webview。
尚未测试,但也许会奏效。
好的,根据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());