DelegateModel DelegateModelGroup under the hood
-
Hello,
I have a QAbstractListModel subclass in c++ and I want to conditionally render items with help of DelegateModel and DelegateModelGroup.
The following code works
model: DelegateModel { model: myModel // Create filter group error, which is empty in the beginning groups: [ DelegateModelGroup { id: errorGroup includeByDefault: false name: 'error' } ] // Filter the delegates to only show items that are in the "error" group filterOnGroup: "error" Component.onCompleted: { // Items contains all items from the model var itemsGroup = items for (var i = 0; i < itemsGroup.count; i++) { // Get the item at i var item = itemsGroup.get(i) // Access the data and do the conditional addGroups if (condition) { itemsGroup.addGroups(i, 1, ["error"]) } } } delegate: Rectangle { ... }
And following code does not work. And by does not work I mean that when accessing some roles from my model in the delegate the value is undefined and I can not assign it to my qml stuff.
Component.onCompleted: { var rowCount = myModel.rowCount() // Remove all items in the default "items" group items.remove(0, rowCount) for (var i = 0; i < rowCount; i++) { var idx = myModel.index(i) if (!idx.valid) { continue } var entry = myModel.data(idx) if (condition) { items.insert(entry, "error") } } }
With the second approach the error group is populated correctly and the delegates accordigly drawn. But there is data missing within the delegate.
The only way I could explain this is by assuming that by filtering on the items which are in the default items group and putting them in the error groups some kind of binding between DelegateModel and myModel stays intact, while doing the other approach something breaks.
Anyone knows any specifics?