1

While appending objects to the ListModel, a key is showing 2 times.

Here's my code:

import QtQuick 2.0
import QtQuick.Window 2.0

Window {
    visible: true
    width: 640
    height: 480

    ListModel {
        id: modelCategory
        Component.onCompleted: {
            var cr = [{"nid":[{"value":"17"}],"uuid":[{"value":"f68wb2f2-11a0-457b-b8f4-a3bf55bea3d5"}],"vid":[{"value":"17"}],"langcode":[{"value":"en"}],"type":[{"target_id":"category","target_type":"node_type","target_uuid":"12d62f50-2d60-4c9f-96fb-0351df3e4223"}],"title":[{"value":"Restaurant "}],"uid":[{"target_id":"2","target_type":"user","target_uuid":"0a4c1442-f9b5-4cdb-96d3-f3a6bb35d5dd","url":"\/user\/2"}],"status":[{"value":"1"}],"created":[{"value":"1477296867"}],"changed":[{"value":"1477297787"}]}]

            populateCategory(cr);
            console.log(JSON.stringify(modelCategory.get(0)))
        }
    }

    function populateCategory(categoryResult) {
        modelCategory.clear()
        for (var i in categoryResult) {
            var obj = {
                _id: categoryResult[i].nid[0].value,
                categoryName: categoryResult[i].title[0].value
            }

            modelCategory.append(obj)
        }
    }
}

I'm calling populateCategory() from a JavaScript file that's appending objects. When print the one of the model items, I get:

{"categoryName":"Restaurant","_id":"17","_id":"17"}

It gives two _ids. Why is that? Its ruining the filtering by QSortFilterProxyModel and even its not showing in any views.

Any suggestions?

jpnurmi
  • 5,716
  • 2
  • 21
  • 37
Husni Abdul Nazer
  • 103
  • 1
  • 1
  • 12
  • 1
    Just wondering if you wouldn't be better off if you used a custom model instead of messing around in JavaScript – Kevin Krammer Nov 02 '16 at 19:21
  • Probably the code you show here is different from that you use. Or maybe this strange array is not the same. And yes, I wonder why do you use so strange JS magic instead of making it simple. – folibis Nov 03 '16 at 07:13
  • It worked when ListModel is instantiated with preloaded keys. Okay, can i know a better way to do this with for JSON like document store. Any references also much appreciated, thanks. – Husni Abdul Nazer Nov 03 '16 at 13:54
  • 1
    I've edited your code to be an actual runnable example, something that should have been done in the first place. The code works for me using Qt 5.7. – Mitch Nov 03 '16 at 15:08

0 Answers0