Results 1 to 5 of 5

Thread: QModelIndex validity during beginRemoveRows

  1. #1
    Join Date
    Apr 2009
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QModelIndex validity during beginRemoveRows

    Hello, I'm having a slight problem with model/proxy/view -combo.

    As I call beginRemoveRows(.., FIRST, LAST) to remove all items from the model I get selectionChanged(..) from the View's SelectionModel (assuming I had some items selected). The receiving slot then results in following functions being called

    Qt Code:
    1. void FileVersionManager::installSelectionChanged(
    2. const QItemSelection& selected, const QItemSelection& deselected)
    3. {
    4. installSet_.listSelectionChanged(selected, deselected); // keep track of # of selected rows
    5.  
    6. if (installSet_.selectedRowCount == 1)
    7. {
    8. hoverInstallInfo_ = false;
    9. setActiveInstallFromFile(installSet_.selectedFile());
    10. }
    11. else
    12. {
    13. hoverInstallInfo_ = true;
    14. setActiveInstallFromFile(installSet_.hoverFile()); // <-- relevant call
    15. }
    16. updateInstallActions();
    17. }
    18.  
    19.  
    20. FileNode* FileSet::hoverFile() const
    21. {
    22. QPoint pnt = view->viewport()->mapFromGlobal(QCursor::pos());
    23. return proxy->mapToFile(view->indexAt(pnt));
    24. }
    25.  
    26. FileNode* FileSetSortFilterProxyModel::mapToFile(const QModelIndex& index) const
    27. {
    28. return model_.mapToFile(mapToSource(index));
    29. }
    30.  
    31. FileNode* InstallSetModel::mapToFile(const QModelIndex& index) const
    32. {
    33. if (index.isValid())
    34. return static_cast<FileNode*>(index.internalPointer());
    35. return 0;
    36. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that the FileNode* I get is not NULL as I'd hope it to be due to beginRemoveRows() call. This will eventually segfault later due to the "deleted" file being set where it shouldn't.

    Is it normal behaviour that the view returns valid QModelIndex even when the underlying model's all rows are being removed ? Or should I just avoid calling indexAt() or any other functions that return indexes from the views until after endRemoveRows() ?

    Thanks.

  2. #2
    Join Date
    Nov 2010
    Posts
    4
    Qt products
    Qt4

    Default Re: QModelIndex validity during beginRemoveRows

    Why can't you get the selections before calling beginRemoveRows?

  3. #3
    Join Date
    Apr 2009
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QModelIndex validity during beginRemoveRows

    Quote Originally Posted by dw View Post
    Why can't you get the selections before calling beginRemoveRows?
    The selection itself is not important. I'm not interested in that because the code where beginRemoveRows() gets called is kind of separate from the view part. It just removes the items from model and informs the model about it.

    But the side effect of the call is that selectionChanged() gets emitted. At this point there is nothing selected and every index is in process being deleted. Still the call view->indexAt(pnt) returns valid index which I'd hope it didn't because the attached data is about to be deleted.

  4. #4
    Join Date
    Nov 2010
    Posts
    4
    Qt products
    Qt4

    Default Re: QModelIndex validity during beginRemoveRows

    Might the selectionChanged() and the actual deletion run asynchronously? If not, that is one runs after another then how could a segfault happen?

    What about checking somehow at the beginning of your installSelectionChanged() method whether a delete process is going on?

  5. #5
    Join Date
    Apr 2009
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QModelIndex validity during beginRemoveRows

    Might the selectionChanged() and the actual deletion run asynchronously? If not, that is one runs after another then how could a segfault happen?
    I think it is in sync, because signal gets emitted before beginRemoveRows returns. The segfault happens because FileNode* (from mouse-hovered index) is used in setActiveInstallFromFile(..) as if it was still valid pointer. Well technically it is but after endRemoveRows() it is really deleted.

    What about checking somehow at the beginning of your installSelectionChanged() method whether a delete process is going on?
    I think something like that is what I'm going to have to do in the end. Or somehow disable the signal before beginRemoveRows(). I'm just still wondering the result of view->indexAt(pnt)

Similar Threads

  1. Replies: 1
    Last Post: 23rd September 2010, 05:45
  2. QAbstractItemModel::beginRemoveRows/endRemoveRows
    By SiS-Shadowman in forum Qt Programming
    Replies: 0
    Last Post: 12th July 2010, 08:56
  3. QModelIndex question
    By waynew in forum Qt Programming
    Replies: 2
    Last Post: 10th February 2010, 01:53
  4. invalid QModelIndex
    By Talei in forum Newbie
    Replies: 1
    Last Post: 18th January 2010, 20:21
  5. QModelIndex problem!
    By landonmkelsey in forum Qt Programming
    Replies: 10
    Last Post: 28th August 2008, 19:46

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.