Skip to content
Advertisement

Limit Qlistview to 1 selected item at most

I try to compel the program to select the 1st item(and only it) in the QListview if user’s selection is more 1 and includes the 1st item. The selectionMode flag is multiselection, which means user can select several items in the listview.

Below is my code:

JavaScript

in the constructor:

JavaScript

But this code does not work. It only deselects the last selection the user made before user selects 1st item instead of deselecting all other items except 1st item. How can I do this?

Additional questions:

  1. is the index in QItemSelection and QItemSelectionModel the same with the index in QStandardItemModel?

like if the index of 1st item in QStandardItemModel is 0, then if it’s been selected, no matter what the sequence is, the index will still be 0 in QItemSelection and QItemSelectionModel. (it seems that online material implies that they are the same..)

  1. it seems that the reset() method works. But why I can still see multiple selections in the ListView?

Advertisement

Answer

It turns out that I should use select() method of QItemSelectionModel rather than reset() method. Below is the code that works.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement