Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML and JSON
QtWS25 Last Chance

QML and JSON

Scheduled Pinned Locked Moved QML and Qt Quick
23 Posts 12 Posters 54.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SimonJudge
    wrote on last edited by
    #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

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tobias.hunger
      wrote on last edited by
      #2

      There are several 3rd party JSON libraries that play nicely with Qt. Could using one of those be an option?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SimonJudge
        wrote on last edited by
        #3

        I assume you mean Javascript libraries. Is there one you know works ok with Qt?

        Thanks

        Simon

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tobias.hunger
          wrote on last edited by
          #4

          Actually I was referring to C++ libraries:-)

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fcrochik
            wrote on last edited by
            #5

            I assume there is (or will be) a better way to do all with javascript but if not you can't find out or wait you can always use the script module on c++ to parse the json output and expose the result to qml as a model.

            Certified Specialist & Qt Ambassador <a href="http://www.crochik.com">Maemo, Meego, Symbian, Playbook, RaspberryPi, Desktop... Qt everywhere!</a>

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tobias.hunger
              wrote on last edited by
              #6

              I am not convinced "all Javascript" is a better way! Javascript is much slower than C++ and with a animation running at 60 frames per second you do not have all that much time to update your state...

              1 Reply Last reply
              0
              • F Offline
                F Offline
                fcrochik
                wrote on last edited by
                #7

                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.

                Certified Specialist & Qt Ambassador <a href="http://www.crochik.com">Maemo, Meego, Symbian, Playbook, RaspberryPi, Desktop... Qt everywhere!</a>

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  DenisKormalev
                  wrote on last edited by
                  #8

                  I'm agreed with Tobias. I think that I will prefer QAbstractItemModel that will parse JSON (with one of libraries Tobias told about) and use it as model for QML ListView instead of doing all work in JS.

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Cezary Tomczak
                    wrote on last edited by
                    #9

                    Parsing JSON is easy and no additional libraries are required, you can just use eval(), JSON structure is a valid javascript code, right?

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      DenisKormalev
                      wrote on last edited by
                      #10

                      Cezary Tomczak, yes, but you need to build model from object you will receive after eval, it will take processor time, not JSON parsing.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SimonJudge
                        wrote on last edited by
                        #11

                        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

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          DenisKormalev
                          wrote on last edited by
                          #12

                          SimonJudge, all depends on your JSON size. If it is not big, than JS way is ok for you. But if it contains a lot of elements, then using c++ will be better.

                          1 Reply Last reply
                          0
                          • L Offline
                            L Offline
                            luizpaulo
                            wrote on last edited by
                            #13

                            Hey a good Qt/C++ JSON parser is avaliable with a LGPL licence: http://qjson.sourceforge.net/
                            Might be what you need.

                            1 Reply Last reply
                            0
                            • F Offline
                              F Offline
                              fcrochik
                              wrote on last edited by
                              #14

                              [quote author="luizpaulo" date="1291470389"]Hey a good Qt/C++ JSON parser is avaliable with a LGPL licence: http://qjson.sourceforge.net/
                              Might be what you need.[/quote]

                              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.

                              Certified Specialist & Qt Ambassador <a href="http://www.crochik.com">Maemo, Meego, Symbian, Playbook, RaspberryPi, Desktop... Qt everywhere!</a>

                              1 Reply Last reply
                              0
                              • D Offline
                                D Offline
                                dridk
                                wrote on last edited by
                                #15

                                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});

                                }
                                @

                                Nothing in Biology Makes Sense Except in the Light of Evolution

                                1 Reply Last reply
                                0
                                • F Offline
                                  F Offline
                                  fcrochik
                                  wrote on last edited by
                                  #16

                                  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...

                                  Certified Specialist & Qt Ambassador <a href="http://www.crochik.com">Maemo, Meego, Symbian, Playbook, RaspberryPi, Desktop... Qt everywhere!</a>

                                  1 Reply Last reply
                                  0
                                  • D Offline
                                    D Offline
                                    dridk
                                    wrote on last edited by
                                    #17

                                    That what I m thinking before! But a a guy said me that, and I trust him ... He said me C++ parsing is faster, but exchanging between binary and script is slower.

                                    So, no matter, the javascript method is a little bit more easy than creating a c++ model ! :)

                                    Nothing in Biology Makes Sense Except in the Light of Evolution

                                    1 Reply Last reply
                                    0
                                    • R Offline
                                      R Offline
                                      remy_david
                                      wrote on last edited by
                                      #18

                                      I am also not so happy with the performances of my C++ model but I can't precisely say what's slowing down.

                                      1 Reply Last reply
                                      0
                                      • AlicemirrorA Offline
                                        AlicemirrorA Offline
                                        Alicemirror
                                        wrote on last edited by
                                        #19

                                        Hi to all,

                                        I see that the interest in json parsing is always present. I tried to manage qjson but it seems too complex for what can be done and I have developed a method that I am experiencing very efficient and reliable. It concerns of a C++ class and a very short function in js to be used by QML. If someone is interested I can explain it in details in a wiki page. For now the method is integrated in a project but it will be one of the new features of Qt-Complex 2.0 that I am preparing.

                                        Take a look to http://projects.developer.nokia.com/pricemyhouse.

                                        Enrico Miglino (aka Alicemirror)
                                        Balearic Dynamics
                                        Islas Baleares, Ibiza (Spain)
                                        www.balearicdynamics.com

                                        1 Reply Last reply
                                        0
                                        • P Offline
                                          P Offline
                                          peppelorum
                                          wrote on last edited by
                                          #20

                                          Please don't use eval when parsing json-data, you are actually running the code, so if something nasty is in the reply you get, well...

                                          I've made a small library that plays nice with QML, for making the whole ajax-thingie a bit easier, you can check it out at https://github.com/peppelorum/ajaxmee

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved