Reputation: 4452
Is there a way to remove multiple rows, which are not consecutive, efficiently from a QAbstractListModel? Inefficient example:
// Single row removal
void remove (int idx) {
beginRemoveRows (noParent (), idx, idx);
// internal remove
endRemoveRows ();
}
// Remove each row by calling beginRemoveRows multiple times
void removeMultiple (const QList<QObject*> &items) {
foreach (auto item, items)
{
int idx = findIndexInternal(item);
beginRemoveRows (noParent (), idx, idx);
// internal remove
endRemoveRows ();
}
}
Regards,
Upvotes: 2
Views: 1691
Reputation: 1450
See Qt's Model-View reference:
This sequence can be used for any structural update in lieu of the more high-level and convenient protected methods. For example, if a model of two million rows needs to have all odd numbered rows removed, that is 1 million discountiguous ranges of 1 element each. It would be possible to use beginRemoveRows and endRemoveRows 1 million times, but that would obviously be inefficient. Instead, this can be signalled as a single layout change which updates all necessary persistent indexes at once.
It seems you can use layoutAboutToBeChanged() and layoutChanged() to signal large alterations to the underlying model. I haven't tested this yet, though.
Update: I currently use this architecture and it does the trick. You don't even have to signal row removal/addition, since this is handled by the layout signal.
Upvotes: 2