Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is it necessary to use C++ code when I want to open a "*.txt" file in my QML project?
Forum Updated to NodeBB v4.3 + New Features

Is it necessary to use C++ code when I want to open a "*.txt" file in my QML project?

Scheduled Pinned Locked Moved General and Desktop
qmlc++file
4 Posts 2 Posters 1.4k Views 2 Watching
  • 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.
  • D Offline
    D Offline
    DidaHarp
    wrote on last edited by p3c0
    #1

    Here is my test code ( I want to use Javascript,but it didn't work ):

    import QtQuick 2.4
    
    Rectangle{
        width: 300;height:width
        MouseArea{
            anchors.fill: parent
            onClicked:parent.getTxt()
        }
    
        function getTxt(){
            var txt = new XMLHttpRequest();
            var op = txt.open("GET","http://www.90lrc.cn/52665/SeCret+scARlet")
            if(op){
                console.log("Yes")
            } else {
                console.log("No")
            }
        }
    }
    
    p3c0P 1 Reply Last reply
    0
    • D DidaHarp

      Here is my test code ( I want to use Javascript,but it didn't work ):

      import QtQuick 2.4
      
      Rectangle{
          width: 300;height:width
          MouseArea{
              anchors.fill: parent
              onClicked:parent.getTxt()
          }
      
          function getTxt(){
              var txt = new XMLHttpRequest();
              var op = txt.open("GET","http://www.90lrc.cn/52665/SeCret+scARlet")
              if(op){
                  console.log("Yes")
              } else {
                  console.log("No")
              }
          }
      }
      
      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by p3c0
      #2

      Hi @DidaHarp and Welcome,

      Is it necessary to use C++ code when I want to open a "*.txt" file in my QML project?

      No. Not necessary for remote files.

      Btw you are doing it incorrectly. XMLHttpRequest calls in QML doesnt have synchronous support too. So no point in checking the status immediately. Try the following :

      function getData() {
          var xmlhttp = new XMLHttpRequest();
          var url = "http://www.90lrc.cn/52665/SeCret+scARlet";
      
          xmlhttp.onreadystatechange=function() {
              if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                  getReponse(xmlhttp.responseText);
              }
          }
          xmlhttp.open("GET", url);
          xmlhttp.send();
      }
      
      function getReponse(response) {
          console.log(response)
      }
      

      157

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DidaHarp
        wrote on last edited by p3c0
        #3

        I am sorry that there is an error in the code that I submitted several hours ago?
        The correct code is:

        Rectangle{
            width: 300;height:width
            MouseArea{
                anchors.fill: parent
                onClicked:parent.getTxt()
            }
        
            function getTxt(){
                var txt = new XMLHttpRequest();
                var op = txt.open("GET","/home/Angelo/Documents/Qt/test/test.qml")
                if(op){
                    console.log("Yes")
                } else {
                    console.log("No")
                }
            }
        }
        

        The original goal is to open a local text file and then print it.
        So can you give me a way to parse local text file without C++ code?

        p3c0P 1 Reply Last reply
        0
        • D DidaHarp

          I am sorry that there is an error in the code that I submitted several hours ago?
          The correct code is:

          Rectangle{
              width: 300;height:width
              MouseArea{
                  anchors.fill: parent
                  onClicked:parent.getTxt()
              }
          
              function getTxt(){
                  var txt = new XMLHttpRequest();
                  var op = txt.open("GET","/home/Angelo/Documents/Qt/test/test.qml")
                  if(op){
                      console.log("Yes")
                  } else {
                      console.log("No")
                  }
              }
          }
          

          The original goal is to open a local text file and then print it.
          So can you give me a way to parse local text file without C++ code?

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @DidaHarp No. QML doesn't provide access to local files. In that case you will have to use C++ API's.

          157

          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