QML ListModel显示重复键

时间:2016-11-02 18:45:19

标签: qml qt5.7

将对象追加到ListModel时,一个键显示2次。

这是我的代码:

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

我从附加对象的JavaScript文件中调用populateCategory()。打印其中一个模型项时,我得到:

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

它提供了两个_ids。这是为什么?它破坏了 QSortFilterProxyModel 的过滤,甚至没有在任何视图中显示。

有什么建议吗?

0 个答案:

没有答案