Hi
SDK: Qt Creator 2.4.1
Target platform N9 and Windows 7
Here is a way to read a text file in qml with help of XMLHttpRequest
In the following example the project is named Ozelot
1. Create a text file
A) Right click on the directory "qml/Ozelot" and choose "Add New"
B) Choose "General"
C) Name the file to something, in the following example it is named to "ChangeLog" (it will automatically get file ending .txt)
D) Press "Next"
E) Let it be "Add to the project: <Implicitly Add"
F) Press Finish
A file have now been created in the "directory" "Other files -> qml/Ozelot ->ChangeLog.txt"
2. Edit the newly created text file and add the following
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<content>
<body>
Don't forget me this weekend!
Detta är för kul
</body>
</content>
3. Create your text component like this
Code:
Text {
id: changeLogArea
visible: true
wrapMode: TextEdit.Wrap
Component.onCompleted: {
var doc = new XMLHttpRequest();
doc.onreadystatechange = function() {
if (doc.readyState === XMLHttpRequest.DONE) {
var a = doc.responseXML.documentElement;
if (a !== null) {
for (var ii = 0; ii < a.childNodes.length; ++ii) {
if (a.childNodes[ii].nodeName === "body")
{
changeLogArea.text = changeLogArea.text + a.childNodes[ii].firstChild.nodeValue;
}
}
}
// else
// changeLogArea.text = "Is null"
}
}
doc.open("GET", Qt.resolvedUrl("ChangeLog.txt"))
doc.send();
}
}
Now the program will find the text file both on the N9 and on Windows 7 thanks to Qt.resolvedUrl() and that the text file is created in the right directory
Note:
Of some strange reasons you need to close down the simulator to make changes in the text file, otherwise it will report "Error disk is full"
PS
One nice extension could be the possibility to add rich text formation to the text file
DS
Regards