My app require reading contents from a file. Can you do it using QML alone? Or should I use C++ for it ?


I suppose the answer is no. QML doesn't provide any API to access local files. You can use it in combination with either javascript to or c++ in Qt. I suppose it depends on what you're trying to open and interpret because QML also will have limited support for parsing the data you read in.

A plain text file being read through javascript

File i/o in c++

|improve this answer|||||

You can read local text files using XMLHttpRequest in QML. However, this lacks all of the features of XMLHttpRequest2 and some of the of version 1, which makes it impossible, as far as I can tell, to read in binary files.

To get around this, I wrote a C++ plugin for QML for reading files. It still can't work with binary files directly, but it gives an option of reading them in base64 encoding. I've released this code into the public domain, so do with it what you will.

|improve this answer|||||

There is built-in file I/O available for QML with the V-Play SDK FileUtils. It works cross-platform on desktop, iOS and Android.

It looks like this:

var documentsData = fileUtils.readFile("subfolder/file.json")

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.