View model for nested structure with list (QAbstractListModel, QAbstractItemModel)
-
Hello!
I have the following structures:struct Topic { QString name; bool available; bool operator==(const Topic& other) const { return name == other.name && available == other.available; } }; struct Lesson { QString name; QString description; QList<Topic> topics; bool operator==(const Lesson& other) const { return name == other.name && description == other.description && topics == other.topics; } };
In QML I have 2 views, the first one would display lesson, description and would also contain another, nested view, which would display list of topics. Something like this:
StackLayout { Repeater { model: LessonModel { } delegate: LessonDelegate { name: model.name description: model.description // topics: model.topics ??? <- topics is model for inner TopicModel? } } }
I'm not really sure how could I achieve that. Until now, I used QAbstractListModel and it was perfectly fine, but for this example looks like this is not enough. I think that maybe QAbstractItemModel could help me, but I'm not sure how could use that in my case?
-
Yes, in this case, you need a hierarchical model since a Lesson contains multiple Topics. The QAbstractListModel is not sufficient because it only supports a flat list. Instead, you should use QAbstractItemModel to represent the hierarchical data structure.