简体   繁体   English

从嵌套模型填充QML ListModel

[英]Fill QML ListModel from nested models

I have the following C++ model structure: 我具有以下C ++模型结构:

Manager             // QAbstractListModel
   ↪ Slot           // QAbstractListModel
       ↪ Processor  // QAbstractListModel
            ↪ name  // Q_PROPERTY

I pass only the Manager reference to QML on instantiation. 我仅在实例化时将Manager引用传递给QML。 I need to fill the ComboBox with Processor names but I don't know how to fill up with this nested structure. 我需要用Processor名称填充ComboBox ,但是我不知道如何用此嵌套结构填充。

Here is the code I plan to have (but is not working now): 这是我计划拥有的代码(但现在无法正常工作):

ComboBox {
    model: Repeater {
        model: manager
        delegate: Repeater {
            model: slot
            delegate:Repeater {
                model: processor
                delegate: ListModel {
                    ListElement {text: name}
                }
            }
        }
    }
}

I know that delegates are for specifying how to display data (and that's why ComboBox doesn't have this one), but I'm out of ideas how to implement this correctly. 我知道代表是用于指定如何显示数据的(这就是为什么ComboBox没有这一点的原因),但是我不知道如何正确实现这一点。

So my question is: how to fill up a ListModel recursively? 所以我的问题是:如何递归填充ListModel

I came up with the following solution to recursively fill a ComboBox: 我提出了以下解决方案以递归方式填充ComboBox:

ComboBox {
    id: comboBox
    model: ListModel {}
    textRole: "processorName"

    Repeater {
        model: manager
        delegate: Repeater {
            model: slot
            delegate: Repeater {
                model: processor
                Component.onCompleted: 
                    comboBox.model.append(
                        {"processorName": model.Processor.Name}
                    );
            }
        }
    }
}

添加到您的QAbstractListModel角色,该角色返回另一个QAbstractListModel。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM