Results 1 to 11 of 11

Thread: problems sorting QAbstractItemModel without QSortFilterProxyModel

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problems sorting QAbstractItemModel without QSortFilterProxyModel

    Yeah. those tree model based bugs can get irritating. I suggest you use ModelTest to track your bug down. It helped me many a times. http://qt-project.org/wiki/Model_Test

  2. #2
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    99
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problems sorting QAbstractItemModel without QSortFilterProxyModel

    Thanks for your help!

    To recreate the crash i'm experiencing, add a node and 10 subnodes. Select the last subnode and press the delete button until the program segfaults.

    I tried to use ModelTest but it doesn't give me any hints unfortunately. Honestly though, i had to modify the test first because it didn't like how the text for each item was dependent on its index. The test assumes an object will have the same text before and after its moved, which is not the case in my example. But after that slight modification, ModelTest doesn't say anything, despite the crash i'm seeing. But the fact that i had to modify ModelTest was what prompted me to ask my question here in the first place, perhaps moving items around in the model the way i am is the reason for my problems, and should be done via say, a QSortFilterProxyModel instead.

  3. #3
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problems sorting QAbstractItemModel without QSortFilterProxyModel

    This thread is 6 years old but appears in Google, so I'll hint to the probable solution for future readers.

    If you move items the way you do it, you will invalidate your view's internal persistentModelIndices for selected items, current item etc. Next time such a persistent index will be fed to your model's data() function, it will segfault.

    For sorting / rearranging items you need to either
    Qt Code:
    1. beginResetModel();
    2. sortItems();
    3. endResetModel();
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. for(row in all items) {
    2. toRow = sortRow(row); // or whatever
    3. beginMoveRows(row, toRow);
    4. moveItem(row, toRow);
    5. endMoveRows();
    6. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QSortFilterProxyModel QAbstractItemModel
    By Qiieha in forum Qt Programming
    Replies: 2
    Last Post: 29th October 2012, 08:01
  2. Replies: 0
    Last Post: 8th March 2011, 12:58
  3. Replies: 4
    Last Post: 2nd March 2011, 09:03
  4. Replies: 2
    Last Post: 8th December 2010, 20:37
  5. [QT4] QTreeView, QAbstractItemModel and sorting
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2006, 20:16

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.