Fill QML ListModel from Nested Models

I have the following C ++ model structure:

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

I only pass Managerthe QML link when creating the instance. I need to populate with ComboBoxnames Processor, but I don't know how to populate this nested structure.

Here is the code I plan to have (but it doesn't work now):

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

I know that delegates are intended to indicate how to display data (and why it ComboBoxdoes not have this), but I am not aware of how to implement this correctly.

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

+4
source share
2 answers

I came up with the following solution for recursively populating 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}
                    );
            }
        }
    }
}
+1
source

QAbstractListModel, QAbstractListModel.

0

All Articles