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



  • 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")
            }
        }
    }
    

  • Moderators

    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)
    }
    


  • 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?


  • Moderators

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.