QML and JSON
Page |
1 |
I want to create a QML model from (downloaded) JSON data. I notice there’s a XMLListModel but no similar mechanism for JSON data. How can I do this?
Thanks
Simon
22 replies
I agree that is will not be the most efficient way but one of the goals for QML must be to make easier for the web developers to start writing qml code.
In theory all that we need is to make a http request that will give us a string and an “evaluateJavascript” function :)
Kidding apart, I would just try to create a qml extension in c++ that would return a “generic” model for any URI. I can only imagine that soon or later “qt” will have to provide a standard implementation for this.
Having done some more research, Cezary is correct. While it would be nice to have a JSONListModel, something similar can be achieved using the sample shown at http://bugreports.qt.nokia.com/browse/QTBUG-12117
After other comments it would be useful to know the performance implications of doing this vs doing it in Qt c++
Simon Judge
Hey a good Qt/C++ JSON parser is avaliable with a LGPL licence: http://qjson.sourceforge.net/
Might be what you need.
Hey a good Qt/C++ JSON parser is avaliable with a LGPL licence: http://qjson.sourceforge.net/ Might be what you need.
I saw it when I was working on a simple json parser but decided against it. Depending on the task you can get the same result w/o any additional dependencies – just using the script module.
Hello here is my point of view! I tested everything! XMLModel, javascript XML parser, C++ side parser etc…
- Do Not use C++ Json parser : Because exchange data between c++ and QML is slow
- Make a Javascript parser ! It’s more easy with eval ! Here is an example :
Json data
- {"index":["all"],"flux":{"all":[{"data":{"title":"boris","icon":"icon.png"}]}}
QML side : main.qml
- import QtQuick 1.0
- import "parser.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.center:parent
- text: title
- }
- }
- }
- }
javascript side : parser.js
- function load() {
- listModel.clear();
- var xhr = new XMLHttpRequest();
- xhr.open("GET","http://data.json",true);
- xhr.onreadystatechange = function()
- {
- if ( xhr.readyState == xhr.DONE)
- {
- if ( xhr.status == 200)
- {
- var jsonObject = eval('(' + xhr.responseText + ')');
- loaded(jsonObject)
- }
- }
- }
- xhr.send();
- }
- function loaded(jsonObject)
- {
- for ( var index in jsonObject.flux.all )
- {
- listModel.append({
- "title" : jsonObject.flux.all[index].data["title"],
- "icon" : jsonObject.flux.all[index].data["icon"]});
- }
- // get directly the json object. Should work but not tested
- //listModel.append({jsonObject.flux.all});
- }
It is a little surprising to me that you found that exchanging data between C++ and QML to be slow.
I can imagine that maybe the C++ xml parser you tried is slow but I can’t imagine creating the model in c++ can be any slower than creating it using Javascript.
Another scenario I can imagine it would slow things is if you are adding elements to a model that is already in use.
I am just curious of the why…
You must log in to post a reply. Not a member yet? Register here!





