PDA

View Full Version : [Qt 4.2.2]Working with QTreeView. Expanding and selection of nodes.



Tamara
28th February 2007, 07:32
Situation: There is a tree with an opportunity to add and remove nodes.
I expand and select some nodes and save references on them. After it I add new node and reset model.
Using saved nodes I want to resore selection and expand nodes which were selected...
But the problem is that they are already invalid. I can't understand at what time they become invalid, but I know that is happens before reset()-calling.
Question: Why?!!!
And how to work properly with QTreeView in my situation?

jpn
28th February 2007, 09:34
Situation: There is a tree with an opportunity to add and remove nodes.
I expand and select some nodes and save references on them. After it I add new node and reset model.
Using saved nodes I want to resore selection and expand nodes which were selected...
But the problem is that they are already invalid. I can't understand at what time they become invalid, but I know that is happens before reset()-calling.
Question: Why?!!!
And how to work properly with QTreeView in my situation?
Reseting the model informs each attached view that all the data has become invalid and has to be fetched again. Which kind of model are you using? Avoid reseting the model, it causes all the selections and expand/collapse states to disappear. It should be enough to call QAbstractItemModel::beginInsertRows() and QAbstractItemModel::endInsertRows() to inform the views about the inserted items. Take a look at this explanation (http://doc.trolltech.com/4.2/model-view-creating-models.html#inserting-and-removing-rows).

You should never store QModelIndexes for later usage. They are temporary objects locating some data in the model in the current situation. They become invalid as soon as the model changes. There is a class called QPersistentModelIndex for keeping persistent references to the model. Just be aware that maintaining them is expensive, and you don't need them for behavior you want.