[Interest] QML ListModels and JSON

Jason H jhihn at gmx.com
Thu Mar 26 15:05:23 CET 2015


Thanks. I saw that it didn't work for me, so I hacked it until it did. 

Below is the end result that actually works:
------------
importQtQuick2.3
importQtQml.Models2.1
import "jsonpath.js"as JSONPath
 
ListModel{
	property string source: ""
	property string json: ""
	property var object:[]
	property string query: ""
 
	onSourceChanged:{
 var xhr= new XMLHttpRequest;
 xhr.open("GET", source);
 xhr.onreadystatechange= function(){
 if( xhr.readyState== XMLHttpRequest.DONE)
 json= JSON.parse(xhr.responseText);
}
 xhr.send();
	}
 
	onJsonChanged:{
 object= JSON.parse(json);
 updateJSONModel(true)
	}
 
	onObjectChanged: updateJSONModel()
	onQueryChanged: updateJSONModel()
 
	function updateJSONModel(){
 clear();
 
 var objectArray= objectArray= JSONPath.jsonPath(object, query);
 for( var key in objectArray){
 var jo= objectArray[key];
 append( jo);
 console.debug("JSONModel: append", JSON.stringify(jo))
}
	}
}

 
--------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Sent: Wednesday, March 25, 2015 at 4:45 PM
From: "Daniel França" <daniel.franca at gmail.com>
To: "Jason H" <jhihn at gmx.com>, "interest at qt-project.org" <interest at qt-project.org>
Subject: Re: [Interest] QML ListModels and JSON

I used the JSONListModel for a project, the code is available under MIT license here: https://github.com/kromain/qml-utils  
Em qua, 25 de mar de 2015 às 20:58, Jason H <jhihn at gmx.com[jhihn at gmx.com]> escreveu:I get back from a Ajax a JSON object with a 'table' from a web query:
[
{id: 1, "category":"1", value:"1"},
{id: 2, "category":"1", value:"2"},
{id: 3, "category":"2", value:"3"},
...
]

What is the easiest way to convert the array to ListElements?
Up to this point, I was copying data using ListModel.append() but I was hoping there was an implicit conversion:

listModel = rows; // where rows = [... the array above... ]

I was also looking for a way to create a model of the distinct values of category. I realize there are sections within a ListView, but I want an actual model.


THanks.
_______________________________________________
Interest mailing list
Interest at qt-project.org[Interest at qt-project.org]
http://lists.qt-project.org/mailman/listinfo/interest



More information about the Interest mailing list