5
votes

Is there a way to get and change the active row in a QTreeView (not QTreeWidget)? By active, I mean the row with the focus highlight, not the selected row. In the paint event, I can use QStyle.State_HasFocus to get the active row, but this doesn't seem to work elsewhere.

3

3 Answers

4
votes

You can get/set the active row with the currentIndex() and setCurrentIndex() functions that you can find in both QTreeView and QItemSelectionModel (the latter is returned by QTreeView.selectionModel()).

And despite its name, the QItemSelectionModel handles the view current item, and the view selection independently.

1
votes

Current item is the one which is indicated by the focus rectangle. You can change it using selectionModel function of the tree view. If you don't want to change currently selected items, pass QtGui.QItemSelectionModel.NoUpdate as a second parameter to setCurrentIndex method. Below is an example:

index = model.index(3, 0);
view.selectionModel().setCurrentIndex(index, QtGui.QItemSelectionModel.NoUpdate)

this should move current item to the item with index 3

hope this helps, regards

0
votes

For me it got nothing here new to ask such question, why because simple; you can use Qt-Designer and create QTreeView and then create line edit, then link them using the action editor, then transform the UI file to Py file, then you will see how things work behind the scene.
You will find this if you try:

QtCore.QObject.connect(self.treeView, QtCore.SIGNAL(_fromUtf8("clicked(QModelIndex)")), self.test)