我正在尝试将本地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" } }HTML文件在qrc中引用,一切都在桌面上按预期工作。
但是,当我部署到Android时,webview无法加载本地文件(尽管如果我在Web上使用URL,它仍然有效)。
我在文档或qt bug跟踪器中找不到任何提示(即,据我所知,它应该可以工作)。
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).
最满意答案
好的,基于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());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());更多推荐
发布评论