可以将一个复杂类型的项目添加到ListModel吗? [英] Can one add a complex type item to ListModel?

查看:188
本文介绍了可以将一个复杂类型的项目添加到ListModel吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个类似ListModel的结构来显示简单状态机的输入.每个输入可能包含几个字符串/整数.因此,我需要ListModel的每个项目都能够存储数据列表(带有输入参数名称的字符串,或带有字符串的字典等).目前,我无法将具有列表属性的项目附加到ListModel.

I would like to have a ListModel-like structure to display inputs of a simple state machine. Each input might consist of several strings/ints. So I need each item of the ListModel to be able to store a list of data (strings with names of the input's parameters, or dictionaries with strings etc). At the moment I cannot append an item with a list property to a ListModel.

所以ListModel看起来像这样:

ListView {
    anchors.fill: parent

    model: ListModel {
        id: listModel
    }

    delegate: Text {
        text: inputs[0]['name']
    }
}

当状态改变时,我想更新模型并添加如下元素:

And when the state changes I want to update the model and append elements like this:

var state = {
    name: "abcd",
    inputs: [{name: 'a'}, {name: 'b'}, {name: 'c'}]
}
listModel.append(state);

当前版本的代码返回错误TypeError: Cannot read property 'name' of undefined.它看不到列表.

Current version of the code returns error TypeError: Cannot read property 'name' of undefined. It does not see the list.

根据此问题ListModel的项目中使用列表可能会出现问题.但这似乎与我的情况无关.也许我需要在QML中以不同的方式使用列表和字典,也许我不得不在委托中(尝试过)或其他内容(建议?)中编写text: inputs[0].name.

According to this question there might be issues with using lists in the items of ListModel. But it seems irrelevant to my case. Maybe I need to use lists and dicts differently in QML, maybe I had to write text: inputs[0].name in delegate (which I tried) or something else (suggestions?).

有人可以建议如何在ListModel中制作或多或少复杂的项目(基本上是标准JSON)吗?目前尚不清楚,因为文档和博客/问题始终都在处理字符串.我错过了一些有用的文档吗?在QML中有哪些好的做法?我应该使用一些自定义对象吗?

Could someone suggest how to make a more or less complex item (basically, it is standard JSON) in a ListModel? It is not clear, since documentation and blogs/questions deal with strings all the time. Is there some helpful documentation which I missed? What are good practices to do it in QML? Should I use some custom objects?

推荐答案

根据根据设计,QML不会如果数组中的元素发生更改,则会产生通知,这将是model-view-delegate设置中的一个显示停止器.

List data can be added to a ListElement, according to the documentation and as you correctly did in your imperative code. However, nested roles are not really arrays. They are ListModels themselves. That's because, by design, QML does not produce a notification if an element of an array changes, which would be a show-stopper in a model-view-delegate setting.

由于嵌套角色是模型,因此可以使用模型的功能.例如,此示例可以正常工作:

Since the nested role is a model, you can use model's functions. For instance, this example works fine:

import QtQuick 2.5
import QtQuick.Window 2.2

Window {
    id: window
    width: 600
    height: 400
    visible: true

    ListView {
        anchors.fill: parent

        model: ListModel {
            id: listModel
        }

        delegate: Text {
            text: name + inputs.get(index % inputs.count).name  // accessing the inner model
        }
    }

    MouseArea {
        anchors.fill: parent
        onClicked: {
            var state = {
                name: "abcd",
                inputs: [{name: 'a'}, {name: 'b'}, {name: 'c'}]
            }
            listModel.append(state);
        }
    }
}

根据您的问题,输入为纯JSON.在这种情况下,请考虑使用 JSONListModel 代替ListModel.它通过JSONPath公开了一组与XMLListModel匹配的API,可能代表您的方案的完美解决方案.

According to your question, the input is plain JSON. In that case, consider the usage of JSONListModel in place of ListModel. It exposes a set of API which matches XMLListModel, via JSONPath, and could possibly represent the perfect solution for your scenario.

这篇关于可以将一个复杂类型的项目添加到ListModel吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆