I was looking to select a row in a QTreeView programmatically, and I found 95% of the answer here.
The select()
method does the job perfectly, except that it doesn't seem to trigger any of the events of clicking on the view.
I found a workaround by calling the needed signal myself - but are there any hints of a method that would emulate a human click and send all the signals associated?
Here's my workaround (in Python) :
oldIndex=treeView.selectionModel().currentIndex()
newIndex=treeView.model().indexFromItem(item)
#indexes stored----------------------------------
treeView.selectionModel().select(
newIndex,
QtGui.QItemSelectionModel.ClearAndSelect)
#selection changed-------------------------------
treeView.selectionModel().currentRowChanged.emit(
newIndex,
oldIndex)
#signal manually emitted-------------------------