QListModel Stringify problem



  • 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?



  • 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?



  • @md2012
    How is your model defined?



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



  • This post is deleted!


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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.