首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Qt5.4 qtwebview:未能在android上从qrc加载本地HTML

Qt5.4 qtwebview:未能在android上从qrc加载本地HTML
EN

Stack Overflow用户
提问于 2015-03-12 21:51:54
回答 2查看 2.3K关注 0票数 3

我试图将本地HTML文件加载到QT5.4.1 QtWebView中:

代码语言:javascript
复制
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跟踪器中找不到任何提示(即,据我所知,它应该能工作)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-16 10:29:36

好的,根据fparmana建议,以下代码将qrc中的所有资源复制到临时本地存储中,然后可以加载:

代码语言:javascript
复制
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());
票数 2
EN

Stack Overflow用户

发布于 2015-03-13 03:42:27

基于这个Load local HTML file into WebView,首先尝试将index.html提取到本地存储目录。然后将url更改为绝对路径。

代码语言:javascript
复制
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。

还没有测试,但也许它会起作用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29020930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档