How do I set the selection in a listview?

pyqt, python, qt

Solution

Finally figured it out myself, here is the answer, assuming you have a listview "myListview", a corresponding model "myQStringListModel" with at least 6 elements (0-5) and a array of indices [1,3,5]:

theIndices = [1,3,5]
theQIndexObjects = [self.myQStringListModel.createIndex(rowIndex, 0, self.coverages_lm) for rowIndex in theIndices]
for Qindex in theQIndexObjects:
    myListview.selectionModel().select(Qindex, QtGui.QItemSelectionModel.Select)

IMO its not very straight forward that you have to use the model to create an index object, but it makes sense I guess.

Problem

I have a listview with a QStringModel and I want to change its to selection programatically. I have a a python list of indices as ints, [1,3,4], that I would like to select. How can I select these indices? Does the listview have a function that will allow me to select a row?

Original source