qt 5.4 qtwebview:无法从android上的qrc加载本地HTML(qt 5.4 qtwebview: failing to load local HTML from qrc on an

编程入门 行业动态 更新时间:2024-10-22 20:28:51
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" } }

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());

更多推荐

本文发布于:2023-07-23 18:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1235689.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   android   qtwebview   qt   qrc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!