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. QListModel Stringify problem
Forum Updated to NodeBB v4.3 + New Features

QListModel Stringify problem

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 2 Posters 1.6k 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.
  • md2012M Offline
    md2012M Offline
    md2012
    wrote on last edited by
    #1

    Hello there Qt people.
    Well today i encountered an strange issue while using list model that i couldn't find any solution for it.
    well i want to extract the data of a ListModel in qml.

    ListModel{
                id:editorModel
                dynamicRoles: true
                onCountChanged: {
                    var datamodel = []
                    for (var i = 0; i < editorModel.count; ++i) {
                        datamodel.push(editorModel.get(i))
                    }
                    console.log(JSON.stringify(datamodel))
                    QJSObject.dataStore_exercises = JSON.stringify(datamodel)
                }
            }
    

    there was no problem using this method until another list was present in my ListModel.
    this means twisted ListModels. the extraction for the twisted list model will fail like the list below by only extracting some property belonging to it not the actual data.
    Example :

    {"count":1,"next":null,"previous":null,"results":[{"id":"5b62b9bb91a4890b58045e2e","title_name":"??? ?? ????","description":"","cover_img":"media/mediaFiles/Plyo Push-up_01.jpg","parameters":[{"key":"?? ???","value_":"10"},{"key":"?? ???","value_":"8"},{"key":"?? ???","value_":"6"}],"resource":[{"instruction":"push up 1","img_name":"Plyo Push-up_01.jpg","picture":"media/mediaFiles/Plyo Push-up_01.jpg"},{"instruction":"push up 2","img_name":"Plyo Push-up_02.jpg","picture":"media/mediaFiles/Plyo Push-up_02.jpg"},{"instruction":"push up 3","img_name":"Plyo Push-up_03.jpg","picture":"media/mediaFiles/Plyo Push-up_03.jpg"}],"tags":["neck"],"date_created":"2018-08-02T12:28:51.832000Z","type":"W"}]}
    

    and the corrupted output

    [{"objectName":"","cover_img":"media/mediaFiles/Plyo Push-up_01.jpg","date_created":"2018-08-02T12:28:51.832000Z","description":"","id":"5b62b9bb91a4890b58045e2e","objectName":"","parameters":{"count":3,"dynamicRoles":true,"objectName":""},"resource":{"count":3,"dynamicRoles":true,"objectName":""},"tags":{"count":1,"dynamicRoles":true,"objectName":""},"title_name":"??? ?? ????","type":"W"},{"objectName":"","cover_img":"media/mediaFiles/Plyo Push-up_01.jpg","date_created":"2018-08-02T12:28:51.832000Z","description":"","id":"5b62b9bb91a4890b58045e2e","objectName":"","parameters":{"count":3,"dynamicRoles":true,"objectName":""},"resource":{"count":3,"dynamicRoles":true,"objectName":""},"tags":{"count":1,"dynamicRoles":true,"objectName":""},"title_name":"??? ?? ????","type":"W"},{"objectName":"","cover_img":"media/mediaFiles/Plyo Push-up_01.jpg","date_created":"2018-08-02T12:28:51.832000Z","description":"","id":"5b62b9bb91a4890b58045e2e","objectName":"","parameters":{"count":3,"dynamicRoles":true,"objectName":""},"resource":{"count":3,"dynamicRoles":true,"objectName":""},"tags":{"count":1,"dynamicRoles":true,"objectName":""},"title_name":"??? ?? ????","type":"W"},{"objectName":"","cover_img":"media/mediaFiles/Plyo Push-up_01.jpg","date_created":"2018-08-02T12:28:51.832000Z","description":"","id":"5b62b9bb91a4890b58045e2e","objectName":"","parameters":{"count":3,"dynamicRoles":true,"objectName":""},"resource":{"count":3,"dynamicRoles":true,"objectName":""},"tags":{"count":1,"dynamicRoles":true,"objectName":""},"title_name":"??? ?? ????","type":"W"},{"objectName":"","cover_img":"media/mediaFiles/Plyo Push-up_01.jpg","date_created":"2018-08-02T12:28:51.832000Z","description":"","id":"5b62b9bb91a4890b58045e2e","objectName":"","parameters":{"objectName":"","count":3,"dynamicRoles":true},"resource":{"objectName":"","count":3,"dynamicRoles":true},"tags":{"objectName":"","count":1,"dynamicRoles":true},"title_name":"??? ?? ????","type":"W"}]
    

    it's a bit too crowded so i point out one diffrence:

    "resource":[{"instruction":"push up 1","img_name":"Plyo Push-up_01.jpg","picture":"media/mediaFiles/Plyo Push-up_01.jpg"},...
    

    to

    "resource":{"count":3,"dynamicRoles":true,"objectName":""},"tags":{"count":1,"dynamicRoles":true,"objectName":""},...
    

    so what do you think guys?

    md2012M 1 Reply Last reply
    0
    • md2012M md2012

      Hello there Qt people.
      Well today i encountered an strange issue while using list model that i couldn't find any solution for it.
      well i want to extract the data of a ListModel in qml.

      ListModel{
                  id:editorModel
                  dynamicRoles: true
                  onCountChanged: {
                      var datamodel = []
                      for (var i = 0; i < editorModel.count; ++i) {
                          datamodel.push(editorModel.get(i))
                      }
                      console.log(JSON.stringify(datamodel))
                      QJSObject.dataStore_exercises = JSON.stringify(datamodel)
                  }
              }
      

      there was no problem using this method until another list was present in my ListModel.
      this means twisted ListModels. the extraction for the twisted list model will fail like the list below by only extracting some property belonging to it not the actual data.
      Example :

      {"count":1,"next":null,"previous":null,"results":[{"id":"5b62b9bb91a4890b58045e2e","title_name":"??? ?? ????","description":"","cover_img":"media/mediaFiles/Plyo Push-up_01.jpg","parameters":[{"key":"?? ???","value_":"10"},{"key":"?? ???","value_":"8"},{"key":"?? ???","value_":"6"}],"resource":[{"instruction":"push up 1","img_name":"Plyo Push-up_01.jpg","picture":"media/mediaFiles/Plyo Push-up_01.jpg"},{"instruction":"push up 2","img_name":"Plyo Push-up_02.jpg","picture":"media/mediaFiles/Plyo Push-up_02.jpg"},{"instruction":"push up 3","img_name":"Plyo Push-up_03.jpg","picture":"media/mediaFiles/Plyo Push-up_03.jpg"}],"tags":["neck"],"date_created":"2018-08-02T12:28:51.832000Z","type":"W"}]}
      

      and the corrupted output

      [{"objectName":"","cover_img":"media/mediaFiles/Plyo Push-up_01.jpg","date_created":"2018-08-02T12:28:51.832000Z","description":"","id":"5b62b9bb91a4890b58045e2e","objectName":"","parameters":{"count":3,"dynamicRoles":true,"objectName":""},"resource":{"count":3,"dynamicRoles":true,"objectName":""},"tags":{"count":1,"dynamicRoles":true,"objectName":""},"title_name":"??? ?? ????","type":"W"},{"objectName":"","cover_img":"media/mediaFiles/Plyo Push-up_01.jpg","date_created":"2018-08-02T12:28:51.832000Z","description":"","id":"5b62b9bb91a4890b58045e2e","objectName":"","parameters":{"count":3,"dynamicRoles":true,"objectName":""},"resource":{"count":3,"dynamicRoles":true,"objectName":""},"tags":{"count":1,"dynamicRoles":true,"objectName":""},"title_name":"??? ?? ????","type":"W"},{"objectName":"","cover_img":"media/mediaFiles/Plyo Push-up_01.jpg","date_created":"2018-08-02T12:28:51.832000Z","description":"","id":"5b62b9bb91a4890b58045e2e","objectName":"","parameters":{"count":3,"dynamicRoles":true,"objectName":""},"resource":{"count":3,"dynamicRoles":true,"objectName":""},"tags":{"count":1,"dynamicRoles":true,"objectName":""},"title_name":"??? ?? ????","type":"W"},{"objectName":"","cover_img":"media/mediaFiles/Plyo Push-up_01.jpg","date_created":"2018-08-02T12:28:51.832000Z","description":"","id":"5b62b9bb91a4890b58045e2e","objectName":"","parameters":{"count":3,"dynamicRoles":true,"objectName":""},"resource":{"count":3,"dynamicRoles":true,"objectName":""},"tags":{"count":1,"dynamicRoles":true,"objectName":""},"title_name":"??? ?? ????","type":"W"},{"objectName":"","cover_img":"media/mediaFiles/Plyo Push-up_01.jpg","date_created":"2018-08-02T12:28:51.832000Z","description":"","id":"5b62b9bb91a4890b58045e2e","objectName":"","parameters":{"objectName":"","count":3,"dynamicRoles":true},"resource":{"objectName":"","count":3,"dynamicRoles":true},"tags":{"objectName":"","count":1,"dynamicRoles":true},"title_name":"??? ?? ????","type":"W"}]
      

      it's a bit too crowded so i point out one diffrence:

      "resource":[{"instruction":"push up 1","img_name":"Plyo Push-up_01.jpg","picture":"media/mediaFiles/Plyo Push-up_01.jpg"},...
      

      to

      "resource":{"count":3,"dynamicRoles":true,"objectName":""},"tags":{"count":1,"dynamicRoles":true,"objectName":""},...
      

      so what do you think guys?

      md2012M Offline
      md2012M Offline
      md2012
      wrote on last edited by md2012
      #2

      Well after a few debugging i found out the problem is with JSON.stringify that extract the twisted list model properties instead of their actual data.
      any other options or work around to Convert a list model to string?

      DiracsbracketD 1 Reply Last reply
      0
      • md2012M md2012

        Well after a few debugging i found out the problem is with JSON.stringify that extract the twisted list model properties instead of their actual data.
        any other options or work around to Convert a list model to string?

        DiracsbracketD Offline
        DiracsbracketD Offline
        Diracsbracket
        wrote on last edited by
        #3

        @md2012
        How is your model defined?

        md2012M 1 Reply Last reply
        0
        • DiracsbracketD Diracsbracket

          @md2012
          How is your model defined?

          md2012M Offline
          md2012M Offline
          md2012
          wrote on last edited by
          #4

          @Diracsbracket
          They are empty list models with dynhamicRoles turned on.

          ListModel{
                      id:editorModel
                      dynamicRoles: true
          }
          

          after retrieving data from server they will be filled with :

          function item_loader(items) {
                  items.map(function(item) {
                          searchList.append(item)
                  })
              }
          

          but as i said none of this steps are the problem.things go wrong after the JSON.stringify.
          Full json that i used for example is in the first post.

          DiracsbracketD 2 Replies Last reply
          0
          • md2012M md2012

            @Diracsbracket
            They are empty list models with dynhamicRoles turned on.

            ListModel{
                        id:editorModel
                        dynamicRoles: true
            }
            

            after retrieving data from server they will be filled with :

            function item_loader(items) {
                    items.map(function(item) {
                            searchList.append(item)
                    })
                }
            

            but as i said none of this steps are the problem.things go wrong after the JSON.stringify.
            Full json that i used for example is in the first post.

            DiracsbracketD Offline
            DiracsbracketD Offline
            Diracsbracket
            wrote on last edited by Diracsbracket
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • md2012M md2012

              @Diracsbracket
              They are empty list models with dynhamicRoles turned on.

              ListModel{
                          id:editorModel
                          dynamicRoles: true
              }
              

              after retrieving data from server they will be filled with :

              function item_loader(items) {
                      items.map(function(item) {
                              searchList.append(item)
                      })
                  }
              

              but as i said none of this steps are the problem.things go wrong after the JSON.stringify.
              Full json that i used for example is in the first post.

              DiracsbracketD Offline
              DiracsbracketD Offline
              Diracsbracket
              wrote on last edited by Diracsbracket
              #6

              @md2012
              So everything works, and there is no real problem? I don't think you can stringify your ListModel in any other way.
              I don't know if doing the stringification in the onCountChanged as you do is the best way though, since you repeat the stringification of the existing items all over again as the count increases.

              1 Reply Last reply
              2
              • md2012M Offline
                md2012M Offline
                md2012
                wrote on last edited by md2012
                #7

                well with a few customization from server side i resolved the issue from serverside by avoiding complex lists.

                DiracsbracketD 1 Reply Last reply
                1
                • md2012M md2012

                  well with a few customization from server side i resolved the issue from serverside by avoiding complex lists.

                  DiracsbracketD Offline
                  DiracsbracketD Offline
                  Diracsbracket
                  wrote on last edited by
                  #8

                  @md2012
                  Good to read! And thanks for your feedback.

                  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