1

I need open a text file and read it's contents line by line and store each lines in a string list in qt quick 2.2.

Can every one help me?

1
  • why did you change from javascript to qml?
    – Rinzwind
    Jun 28, 2014 at 18:03

1 Answer 1

3

QML provides a partially-functional version of XMLHttpRequest. It's good enough to load text files from the local files system, but that's about it. An example:

var request = new XMLHttpRequest()
request.open('GET', 'test.txt')
request.onreadystatechange = function(event) {
    if (request.readyState == XMLHttpRequest.DONE) {
        lines = request.responseText.split('\n')  // Assuming you've defined
    }                                             // lines as a property
}
request.send()

Note that only a portion of the XMLHttpRequest1 interface is supported. Notably, only asynchronous requests are allowed. I've written some more details here.

2
  • How do I actually use this data in my qml file ? lets say... I want to use that text from text.file as text in my Text component in qml ? How do I do that ? I always get [undefined], and when I do request.toString() I get [object Object]
    – Majster-pl
    Dec 21, 2014 at 22:45
  • As the example shows, the data is available in request.responseText, but only after request.readyState advances to XMLHttpRequest.DONE. XHRs are always asynchronous in QML, so you have to handle this in a callback. Dec 22, 2014 at 19:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .