如何读取 QML 中的 FILE 内容?
我只需要类似于 Fstream 的东西来读取 QML 中的文件 IO。为什么没有文件IO?
I just need something similar to Fstream to read file IO in QML. Why is there no file IO?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
QML 没有内置文件 I/O。但是 - 从你帖子的语气来看 - 你已经知道了。
如何读取 QML 中的 FILE 内容?
您可以使用 C++ 扩展 QML 的功能。
Qt 参考文档中的QML 编程入门教程向您展示了如何构建文本编辑器。这包括使用 C++ 的文件 I/O。
为什么没有文件I/O?
因为QML是基于JavaScript的,而JavaScript也没有内置的文件I/O。
QML 被设计为构建用户界面的(简单)方法。您需要一个实际的程序来完成其余的工作。
QML has no built-in file I/O. But - judging from the tone of your post - you already knew that.
How do I read in FILE contents in QML?
You can extend QML's functionalities using C++.
The Getting Started Programming with QML tutorial from the Qt Reference Documentation shows you how to build a text editor. This includes file I/O using C++.
Why is there no file I/O?
Because QML is based on JavaScript, and JavaScript has no built-in file I/O either.
QML is designed as an (easy) way to build a user interface. You need an actual program to do the rest.
Felgo SDK(以前称为 V-Play)有可用于 QML 的内置文件 I/O <代码>FileUtils。它可以在桌面、iOS 和 Android 上跨平台运行。更多信息和示例也位于 最新博客文章。
它看起来像这样:
There is built-in file I/O available for QML with the Felgo SDK (formerly V-Play)
FileUtils
. It works cross-platform on desktop, iOS and Android. More info and examples are also in the latest blog post.It looks like this:
您想读取该文件做什么?...如果它是简单的数据..那么您最好使用 QML Offline storage API。 在此处查找该部分。
如果您想在应用程序中部署数据库,请阅读此对话。
如果您确实想继续阅读该文件,请学习 C++ 并将您的代码公开给 QML。那么,这超出了我的回答范围。
What do you want to read the file for?... if its simple data.. then you are probably better off using QML Offline storage API. Look for that section here.
If you want to deploy a db with your application read this conversation.
If you really want to read the file still, learn C++ and expose your code to QML. That, then, is beyond the scope of my answer.
我知道这是一个老问题,但您可能仍然有兴趣回答。
这是: 阅读qml (Qt Quick) 中的 .txt 或 .csv 文件中的行
简而言之,您在这里解释了如何读取 QML 中的文件: http://www.developer.nokia.com/Community/Wiki/Reading_and_writing_files_in_QML
全部您需要的是用 C++ 扩展 QML。
I know this is old question but you might be still interested in answer.
Here it is: Reading a line from a .txt or .csv file in qml (Qt Quick)
In short, you have here explained how to read files in QML: http://www.developer.nokia.com/Community/Wiki/Reading_and_writing_files_in_QML
All you need is to extend QML with C++.
如果您的文件是纯文本,您可以使用 XMLHttpRequest。例如:
If your file is plain text you can use XMLHttpRequest. For example: