Symbian-Developers
The Official Developers Section => Symbian Software Development Discussions => The Developers Section => Qt Application Development => Topic started by: symster on February 01, 2014, 03:52:33 pm
-
This tutorial is about how to use QJson to implement a feature where users can check for update.This topic could be quite late now because nokia store already close to developers but i hope some developers could still find something useful from this topic.If you want to use QJson to display data,refer to the nokia developer site link.Some of the code example is from http://developer.nokia.com/community/wiki/How_to_use_QJson_to_easy_manage_JSON_objects_with_Qt
Note :
QJson does not work if the json file is in your project.QJson is for online use only.
Needed Files :
QJson - Download the source code from the link above
Json file -
Example below.The "version" is the object while the "1.12.0" is array for the object.Json file can be hosted on your preferred server.
{
"version": "1.12.0"
}
Include QJson Into Project :
In your .pro file
include(./qjson/json.pri)
In your main.cpp
#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QtDeclarative>
#include "qjson/qjson.h"
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QApplication app(argc, argv);
//THIS IS TO REGISTER THE PROJECT SO IT CAN BE USE IN QML
qmlRegisterType<QJson>("QJson", 1, 0, "QJson");
QDeclarativeView view;
view.setSource(QUrl("qml/MyProject/main.qml"));
view.window()->show();
return app.exec();
}
Qml Code
Import QJson 1.0
//TEXT FOR APP VERSION
property string decoding: "1.12.5"
//VERSION STRING FROM JSON
Text {
id: encoding
visible: false
}
QJson {
id: json
onFailure: {
errordialog.open();
}
}
//DO ANYTHING YOU WANT TO CALL THIS FUNCTION TO START CHECKING
function runcheckupdate() {
var update = json.parse("http://yourdomain.com/test.json");
encoding.text = update['version']; //VERSION IS THE JSON OBJECT
matchstring();
}
//FUNCTION FOR MATCHING APP STRING AND STRING FROM JSON
function matchstring() {
if (encoding.text === decoding)
noupdatedialog.open()
else {
updateavailabledialog.open();
}
}
Known Bugs :
-The original QJson show some ugly s60 dialog when there is an error so i modified the project and add new signal handler "onFailure".If there is a server error or internet connection is not available,"failure" signal is emitted so use the handler to do some nice notification. :)
-For some reason the UI is unresponsive if the app is fetching data.Example busy indicator stop spinning if the app is fetching the data.Never have time to fix it.
-
QJson is a convenient tool in qt c++ indeed. But in qml part, using XMLHttpRequest and JSON.parse() is a more common way.
-
:D.I know qml itself support json.Since i am unable to get the qml JSON.parse work with link so i just decided to use QJson.