ListView with shift/ctrl-based multiple selection
-
wrote on 10 Jul 2020, 10:55 last edited by
I am trying to introduce some 'standard' multiselect paradigms into an existing
ListView
-based component - i.e., so that shift+leftMouse selects from previous selection to current position, control+leftMouse toggles the selection of the current item without clearing existing selections, etc.I happened across
DelegateModel
andDelegateModelGroup
and wondered if this might help. In fact the example in the documentation of theDelegateModel.group
property looked similar to what I would need for the control+select case but shift selection is less obvious - I would need to add entries into the group that represent the implied selected range and it's not obvious to me how to do that.I suspect I am barking up the wrong tree with
DelegateModelGroup
. I have also just come acrossItemSelectionModel
and am about to have a play with that to see if I can build something on that. There seems to be surprisingly little information out there about multi-select andListView
so I thought it might be worth asking here to see if anyone has any experience of implementing something similar, or has any ideas or suggestions.Thanks.
-
wrote on 10 Jul 2020, 12:14 last edited by
Well, it looks like
ItemSelectionModel
could be useful but I have to say that the documentation is pretty rubbish. In any event, it is not working as I would have expected it to.The following is all I am doing to introduce it at the moment:
ItemSelectionModel { id: selectionModel model: myModel } ...
MouseArea { ... onClicked: { if (mouse.modifiers & Qt.ControlModifier) { console.log("selecting ", myModel.data(myModel.index(index, 0)).value, index) selectionModel.select(myModel.index(index, 0), ItemSelectionModel.SelectCurrent); console.log("selected indexes", selectionModel.selectedIndexes, "length=", selectionModel.selectedIndexes.length) } } }
ItemSelectionModel.SelectCurrent
is supposed to be equivalent toItemSelectionModel.Select | ItemSelectionModel.Current
but I have tried both. The way this is supposed to work as I understand it is that each selected index should be added to the current selection but I only ever see one selection inselectedIndexes
in the output above.Please can someone suggest where I am going wrong here? Thanks.
-
wrote on 10 Jul 2020, 16:01 last edited by
It looks like I was misunderstanding what was meant by "current selection". From the docs for QItemSelectionModel:
"The QItemSelectionModel takes a two layer approach to selection management, dealing with both selected items that have been committed and items that are part of the current selection. The current selected items are part of the current interactive selection (for example with rubber-band selection or keyboard-shift selections)."
Using the
Current
flag causes a new current selection to be created with the new item, throwing away the other one. AvoidingCurrent
causes the selected item to be added to the committed selection, which is what I was expecting.
1/3