简体   繁体   中英

Fill QML ListModel from nested models

I have the following C++ model structure:

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

I pass only the Manager reference to QML on instantiation. I need to fill the ComboBox with Processor names but I don't know how to fill up with this nested structure.

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.

So my question is: how to fill up a ListModel recursively?

I came up with the following solution to recursively fill a 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。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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