How do I read in FILE contents in QML?

23,271

Solution 1

If your file is plain text you can use XMLHttpRequest. For example:

var xhr = new XMLHttpRequest;
xhr.open("GET", "mydir/myfile.txt");
xhr.onreadystatechange = function() {
    if (xhr.readyState == XMLHttpRequest.DONE) {
        var response = xhr.responseText;
        // use file contents as required
    }
};
xhr.send();

Solution 2

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++.

Solution 3

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.

Solution 4

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:

var documentsData = fileUtils.readFile("subfolder/file.json")
Share:
23,271
Christopher Peterson
Author by

Christopher Peterson

I am a software engineer at Garmin International. I recently graduated from University of Advancing Technology. I am interested in AI and automation, the thought of something learning, collecting data, moving, etc. on its own thrills me and I believe we can advance so many things in society because we can create tools that are more efficient than humans.

Updated on December 07, 2020

Comments

  • Christopher Peterson
    Christopher Peterson over 3 years

    I just need something similar to Fstream to read file IO in QML. Why is there no file IO?