Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read in FILE contents in QML?

Tags:

qt

qt-quick

qml

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

like image 894
Christopher Peterson Avatar asked Oct 14 '11 21:10

Christopher Peterson


People also ask

How do I read a QML file?

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

How do I use a QML file in Qt?

Creating and Running QML Projects For simple UI files such as this one, select File > New File or Project > Application (Qt Quick) > Qt Quick Application - Empty from within Qt Creator. Pressing the green Run button runs the application. You should see the text Hello, World! in the center of a red rectangle.

What is difference between Qt and QML?

QML is the language; its JavaScript runtime is the custom V4 engine, since Qt 5.2; and Qt Quick is the 2D scene graph and the UI framework based on it. These are all part of the Qt Declarative module, while the technology is no longer called Qt Declarative.

Is QML compiled to C++?

With the QML type compiler, you will be able to compile your object structure to C++. Each QML component becomes a C++ class this way.


2 Answers

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.

like image 145
Dennis Avatar answered Oct 16 '22 03:10

Dennis


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();
like image 28
funkybro Avatar answered Oct 16 '22 02:10

funkybro