Results 1 to 2 of 2

Thread: [Qt 4.2.2]Working with QTreeView. Expanding and selection of nodes.

  1. #1
    Join Date
    Feb 2007
    Location
    Russia, Novosibirsk
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default [Qt 4.2.2]Working with QTreeView. Expanding and selection of nodes.

    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?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: [Qt 4.2.2]Working with QTreeView. Expanding and selection of nodes.

    Quote Originally Posted by Tamara View Post
    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.

    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.
    J-P Nurmi

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.