Results 1 to 3 of 3

Thread: QPersistentModelIndex problem

  1. #1
    Join Date
    Aug 2009
    Posts
    23
    Thanks
    3

    Default QPersistentModelIndex problem

    Hi all,

    Here is my problem ... I have a widget containing 4 tree views connected to the same model. I store QModelIndex to be able to restore expanded items when I switch from a tree view to another.
    In order to do it, I want to use QPersistentModelIndex because if I delete a QModelIndex from my model, the QPersistentModelIndex would become invalid. At least, it's what I understood from the documentation.

    Here is my code:

    Qt Code:
    1. /**
    2.  * List of expanded items.
    3.  */
    4. QModelIndexList m_expandedItemList;
    5.  
    6. //================================================================
    7. void QResourceTree::addExpandedItem(const QModelIndex& index)
    8. {
    9. QResourceProxyModel * proxyModel = (QResourceProxyModel*)model();
    10. QModelIndex sourceIndex = proxyModel->mapToSource(index);
    11. m_expandedItemList.append(QPersistentModelIndex(sourceIndex));
    12. }
    13.  
    14. //================================================================
    15. void QResourceTree::restoreExpandedItems() {
    16.  
    17. if (!m_expandedItemList.empty()) {
    18.  
    19. /* Restore expanded items */
    20. QModelIndexList::iterator it;
    21. for (it=m_expandedItemList.begin() ; it != m_expandedItemList.end(); it++) {
    22.  
    23. if ((*it).isValid()) {
    24.  
    25. QResourceProxyModel * proxyModel = (QResourceProxyModel*)model();
    26. QModelIndex index = proxyModel->mapFromSource(*it);
    27.  
    28. if (index.isValid() && !isExpanded(index)) {
    29. this->expand(index);
    30. }
    31. }
    32. }
    33. }
    34. }
    To copy to clipboard, switch view to plain text mode 

    Code for deletion:

    Qt Code:
    1. // Is it the right order?
    2. 1/ m_root->removeChildren(index.row());
    3. 2/ changePersistentIndex(index, QModelIndex());
    To copy to clipboard, switch view to plain text mode 

    Anytime I delete an expanded item, when I restore the expanded item list, it crashes because my QPersistentModelIndex is still valid.
    How do we get a persistent index from a model index? I thought it would be by using the constructor, maybe I am wrong?
    Anybody has an example showing how to use QPersistentModelIndex?


    Thanks,
    Lionel

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPersistentModelIndex problem

    you must not store the perstistent model indexes in a QModelIndexList; use
    Qt Code:
    1. QList<QPersistentModelIndex>
    To copy to clipboard, switch view to plain text mode 
    instead

    HTH

  3. The following user says thank you to caduel for this useful post:

    laugusti (2nd November 2009)

  4. #3
    Join Date
    Aug 2009
    Posts
    23
    Thanks
    3

    Default Re: QPersistentModelIndex problem

    Thank you Caduel,

    It works perfectly now!

    Regards,
    Lionel

Similar Threads

  1. Problem with QAbstractListModel
    By eekhoorn12 in forum Qt Programming
    Replies: 3
    Last Post: 26th August 2009, 14:26
  2. Replies: 1
    Last Post: 23rd April 2009, 09:05
  3. Replies: 19
    Last Post: 3rd April 2009, 23:17
  4. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  5. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36

Tags for this Thread

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.