QListView extended selection: shift problem
I have implemented a dialog with a QListView in it. I use the selectionChanged signal to update the selectionModel underlying it. However, I get unexpected behaviour when pressing the Shift key. To reproduce:
- Add 4 items to the list view
- Select the first item in the list (works fine, i.e. selects the item)
- Pressing Shift key, select all 4 items (works fine, i.e. selects all items)
- Still pressing Shift key, select the second item (no event is triggered! I expect first and second items to be selected)
The behaviours for single selection and selection using the Control key both work as expected. Has anyone come across this problem before?
May you chose the wrong selcetionMode. It seems you would want to set to
Thanks for the reply - that's the selection mode that I have.
What you describe should work, is the expected behaviour of the shift key and normally works. I just tested it with one of my own ListWidgets (ListViews should not be different here).
Maybe you inadvertantly used the CTRL key in between. This can disturb the normal working of the shift key up to a point where the shift key does no longer seem to work at all. Though this is probably not the case.
Maybe you connected the model to the QListView and additionally did some selection transfer by yourself, something that the model already should do on its own (I suppose). And both actions interfer with each other.
You are right, I do use the model selection to select items and possibly this is causing trouble. I have checked and yes, the QListView itself behaves normally as expected so it's not Qt's problem but mine. However, I need to use the model selection by design so I'll have to find a way to get similar behaviour using it :(
Sorry for the bother.
I think I know where the problem is. I was selecting the items one by one (from the model) instead of using QItemSelection as the widget does. Changing this seems to have solved the issue!
Thanks for the chat - it helped me realise where the problem was :)