Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to read an external file?

How to read an external file?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 3 Posters 647 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    khachkara
    wrote on last edited by khachkara
    #1

    Hi everyone. I wish to give as an argument to a text field of a TextArea an external text file? Is it possible, if yes how can I do it?

    1 Reply Last reply
    1
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Read the file on C++ side and pass the text to QML. For example: https://doc.qt.io/qt-5/qtqml-cppintegration-topic.html

      (Z(:^

      1 Reply Last reply
      3
      • K Offline
        K Offline
        khachkara
        wrote on last edited by
        #3

        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.

        sierdzioS 1 Reply Last reply
        0
        • K khachkara

          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.

          sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

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

          (Z(:^

          1 Reply Last reply
          0
          • H Offline
            H Offline
            HenkKalkwater
            wrote on last edited by
            #5

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

            K 1 Reply Last reply
            1
            • H HenkKalkwater

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

              K Offline
              K Offline
              khachkara
              wrote on last edited by
              #6

              @HenkKalkwater thank you a lot. It works perfectly.
              ScreenShot886.jpg

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved