oKcerG / SortFilterProxyModel

A nicely exposed QSortFilterProxyModel for QML
MIT License
298 stars 101 forks source link

SortFilterProxyModel and ItemSelectionModel #73

Open novoslinux opened 4 years ago

novoslinux commented 4 years ago

Hi!

I am looking for a way to do ItemSelectionModel with SortFilterProxyModel. As the QML ItemSelectionModel does not have a selectAll (can only select single Indices) I pass the ItemSelectionModel to c++ c++item_selection_model->select(QItemSelection(startRowIndex, endRowIndex), QItemSelectionModel::Select); So far so good all the indices that should be selected will be selected and //QML item_selection_model.rowIntersectsSelection(index, 0) does the correct thing but once you filter or sort something the order is messed up (rowIntersectsSelection will report indices that should not be selected and not report indices that should be selected).

@oKcerG what are your thoughts on how to handle such case in oKcerG/SortFilterProxyModel?

1) There is the option to just clear everything on filter or sort changes. 2) Select items non-contiguously would be incredibly slow.

oKcerG commented 4 years ago

Hi!

Do you use the ItemSelectionModel on the source model? Did you forgot to map the indexes to the source model?

I experimented a bit and it seemed to work here, with such a delegate and a ItemSelectionModel on the source model:

ItemDelegate {
    readonly property var modelIndex: proxyModel.mapToSource(proxyModel.index(model.row, 0))
    onClicked: selectionModel.select(modelIndex, ItemSelectionModel.Toggle)
    highlighted: selectionModel.selectedIndex.indexOf(modelIndex) !== -1
}

It also works with : highlighted: selectionModel.selectedIndexes, selectionModel.rowIntersectsSelection(proxyModel.mapToSource(model.index), modelIndex.parent) but that's more verbose and hacky with the binding to selectedIndexes to force the reevaluation