[PyQt] How to select a row in treeview
IT
it at geolab.org.uk
Thu Jun 4 18:19:29 BST 2009
Hi all, a newbie and throughly enjoying pyqt; but and a little problem.
I've got a treeview/model which is loaded via json.
I want to select a row in code, eg after a refresh of data to restore
the previous row or to select a new one.
The problem I'm having is that I can seem to be able to select a row
properly, it ends up being a single cell which throws and error in the
selectionChanged() handler. Am also a bit confused how to create
modelIndex's that span a row.
Can anyone help please, tia
pete
##########################################################################
## Model
self.model = QtGui.QStandardItemModel(0, 3, self)
self.model.setHeaderData(wTestCategoryColumns.test_category_id,
QtCore.Qt.Horizontal, QtCore.QVariant("#"))
self.model.setHeaderData(wTestCategoryColumns.test_category_item,
QtCore.Qt.Horizontal, QtCore.QVariant("Item"))
self.model.setHeaderData(wTestCategoryColumns.test_category,
QtCore.Qt.Horizontal, QtCore.QVariant("Test Category"))
##########################################################################
## Tree
self.tree = QtGui.QTreeView()
#self.tree.setModel( self.proxyModel )
self.tree.setModel( self.model )
self.tree.setSortingEnabled(True)
self.tree.sortByColumn( 2 )
self.tree.header().setStretchLastSection( True )
self.tree.setRootIsDecorated(False)
self.tree.setAlternatingRowColors(True)
self.tree.setSelectionBehavior( QtGui.QAbstractItemView.SelectRows )
self.tree.setSelectionMode(QtGui.QTreeView.ExtendedSelection)
sm = self.tree.selectionModel()
self.connect(sm, QtCore.SIGNAL('selectionChanged(const
QItemSelection&,const QItemSelection&)'), self.on_selection_change)
##########################
## Selection change
def on_selection_change(self, newSelection, oldSelection):
### If its selected below, then this bombs out as the indexes
ain't there ie complete row
print "new=", newSelection.indexes()
self.params['test_category_id'] = int( self.model.itemFromIndex(
newSelection.indexes()[0] ).text() )
self.params['test_category'] = self.model.itemFromIndex(
newSelection.indexes()[2] ).text()
### LOAD .. and this is where one is confused .....
if row_to_select:
modelIndexL = self.model.createIndex( row_to_select, 0)
modelIndexR = self.model.createIndex( row_to_select, 2)
#print "mi=", modelIndexL, modelIndexR
self.tree.selectionModel().select(QtGui.QItemSelection(modelIndexL,
modelIndexR), QtGui.QItemSelectionModel.Columns)
#self.tree.selectionModel().setCurrentIndex( modelIndexL ,
QtGui.QItemSelectionModel.Rows ) # < tried Columns/Select etc ;-(
More information about the PyQt
mailing list