I've just started working on QML and it looks promising because I really like C. While experimenting, I came to a point where I had to update a ListModel from a PHP service using an Ajax request. I referred to this link, but I can't seem to make it work. My code follows.
try.js:
function load() {
listModel.clear();
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://<url>/service_newsletter.php?method=news", true);
xhr.onreadystatechange = function() {
if(xhr.readyState == xhr.DONE){
if(xhr.status == 200) {
var jsonData = JSON.parse(xhr.responseText);
for(var index in jsonData.data.posts) {
listModel.append({
"text": jsonData.data.posts[index].text,
"description": jsonData.data.posts[index].description});
alert(jsonData.data.posts[index].text);
}
}
}
}
xhr.send();
}
QML Code:
import QtQuick 1.1
import "try.js" as JS
Item {
id:root
width: 360
height: 640
Component.onCompleted: JS.load()
ListModel {
id:listModel
}
ListView {
id:view
anchors.fill:parent
model : listModel
delegate: Rectangle {
width:parent.width
height:80
Text {
anchors.centerIn:parent
text: text
}
}
}
}
NOTE: using Necessitas to deploy Qt apps on Android!
NOTE 2: the format of the JSON data from my server is similar to the one given in the example link above.