How to read an external file?
-
Read the file on C++ side and pass the text to QML. For example: https://doc.qt.io/qt-5/qtqml-cppintegration-topic.html
-
Thank you for your response and a link, but I am a newcomer in programming and don't sure is it possible for me to do it in C++. I thought that it was possible to do it in QML.
@khachkara said in How to read an external file?:
I thought that it was possible to do it in QML.
No, or at least I don't know of any built-in way.
Don't be afraid of C++ though, it's really not that hard, and Qt documentation specifies exactly how to do it.
-
@sierdzio said in How to read an external file?:
No, or at least I don't know of any built-in way.
It is possible by sending an XMLHttpRequest to a file URI, for example:
import QtQuick 2.15 Item { property string fileContents: "" Component.onCompleted: { var request = new XMLHttpRequest(); request.onreadystatechange = function() { console.log("Ready state changed: %1".arg(request.readyState)); if (request.readyState == XMLHttpRequest.DONE) { fileContents = request.responseText; } } request.open("GET", "file:/etc/os-release", true); request.send(); console.log("Sending request"); } Text { text: fileContents == "" ? "<Still loading>" : fileContents; } }
I cannot find the documentation for this though, it´s something I heard someone say on a forum or IRC once and it apparently still works.
Writing by sending a PUT-request with data to a file URL is possible as well, but deprecated and potentionally dangerous.
-
@sierdzio said in How to read an external file?:
No, or at least I don't know of any built-in way.
It is possible by sending an XMLHttpRequest to a file URI, for example:
import QtQuick 2.15 Item { property string fileContents: "" Component.onCompleted: { var request = new XMLHttpRequest(); request.onreadystatechange = function() { console.log("Ready state changed: %1".arg(request.readyState)); if (request.readyState == XMLHttpRequest.DONE) { fileContents = request.responseText; } } request.open("GET", "file:/etc/os-release", true); request.send(); console.log("Sending request"); } Text { text: fileContents == "" ? "<Still loading>" : fileContents; } }
I cannot find the documentation for this though, it´s something I heard someone say on a forum or IRC once and it apparently still works.
Writing by sending a PUT-request with data to a file URL is possible as well, but deprecated and potentionally dangerous.
@HenkKalkwater thank you a lot. It works perfectly.