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?
QtWS25 Last Chance

How to read an external file?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 3 Posters 646 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 29 Mar 2021, 07:15 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
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 29 Mar 2021, 07:41 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 29 Mar 2021, 08:03 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.

        S 1 Reply Last reply 29 Mar 2021, 08:22
        0
        • K khachkara
          29 Mar 2021, 08:03

          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.

          S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 29 Mar 2021, 08:22 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 29 Mar 2021, 13:05 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 30 Mar 2021, 07:42
            1
            • H HenkKalkwater
              29 Mar 2021, 13:05

              @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 30 Mar 2021, 07:42 last edited by
              #6

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

              1 Reply Last reply
              0

              5/6

              29 Mar 2021, 13:05

              • Login

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