• May 02, 2024, 05:09:05 am

Author Topic: Use QJson to implement check for update function  (Read 3306 times)

0 Members and 1 Guest are viewing this topic.

Offline symster

  • Developer
  • Happy Member
  • ****
  • Posts: 214
  • Reputation: 6
  • Symbian Power User
  • Current Phone: : Nokia N8 Soon Nokia 808
Use QJson to implement check for update function
« 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.
Code: [Select]
{
    "version": "1.12.0"
}

Include QJson Into Project :

In your .pro file
Code: [Select]
include(./qjson/json.pri)
In your main.cpp
Code: [Select]
#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
Code: [Select]

    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.
« Last Edit: February 01, 2014, 04:00:50 pm by symster »

Offline yeatse

  • Developer
  • Viewer
  • ****
  • Posts: 8
  • Reputation: 6
  • Symbian Power User
  • Current Phone: : 808
Re: Use QJson to implement check for update function
« Reply #1 on: February 06, 2014, 07:06:03 am »
QJson is a convenient tool in qt c++ indeed. But in qml part, using XMLHttpRequest and JSON.parse() is a more common way.

Offline symster

  • Developer
  • Happy Member
  • ****
  • Posts: 214
  • Reputation: 6
  • Symbian Power User
  • Current Phone: : Nokia N8 Soon Nokia 808
Re: Use QJson to implement check for update function
« Reply #2 on: February 06, 2014, 05:02:25 pm »
 :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.