pythonpyside6qlistview

How to get MultiSelection in QListView to trigger a click event


Overview: I have a QListView that displays the names of cities. I use MultiSelection as my selection type so the user can single click on a line to select a city, or multiple cities, without having to use Ctrl. MultiSelection allows the user to click and drag over items in the QListView, but it doesn't trigger a click event, so in another window where I display ONLY the user's selected items, it doesn't update to show what the user just selected. If the user then selects another city, then the click event is triggered and the full list of selected indices gets sent to the QListView object that displays the user's selections.

Problem: I need to be able to ensure that a click event occurs following the users drag-selection, so that the list displaying the selected indices is updated.

I didn't include any code because you either know or you don't know how this is done.


Solution

  • As @musicmante says, using selectionModel and selectionChanged is the way to go, but keep in mind that every time you invoke the setModel method, it resets the selectionModel. So anywhere you're using setModel, it would need to have this after it:

    yourQListView.setModel(to_whatever)
    yourQListView.selectionModel().selectionChanged.connect(theMethodYouWantToRun)