Results 1 to 5 of 5

Thread: Switching/moving rows in a model

  1. #1
    Join Date
    Jan 2007
    Location
    Augsburg, Germany
    Posts
    75
    Thanks
    4
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Switching/moving rows in a model

    Hello,

    I'm trying to implement "move up" and "move down" for items in my model. I'm not using a QSortFilterProxyModel and I won't.

    Is there a working way to do this?

    From my understanding what the docs tell me, I would have to exchange the first row index with the second one and vice versa. But it won't work.
    Qt Code:
    1. emit layoutAboutToBeChanged();
    2.  
    3. QModelIndex from = ...;
    4. QModelIndex to = ...;
    5.  
    6. changePersistentIndex(from, to); // This call overwrites and does not exchange?
    7. changePersistentIndex(to, from);
    8.  
    9. emit layoutChanged();
    To copy to clipboard, switch view to plain text mode 

    Thanks.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Switching/moving rows in a model

    try something like this
    Qt Code:
    1. bool Model::moveItem(const QModelIndex &fromParent, int fromRow,
    2. const QModelIndex &toParent, int toRow, bool up)
    3. {
    4. ...
    5. QModelIndex to = index(toRow, 0, toParent);
    6. QModelIndex from = index(fromRow, 0, fromParent);
    7.  
    8. QPersistentModelIndex pCurrent = from;
    9.  
    10. changePersistentIndex(pCurrent, index(toRow, 0, toParent));
    11.  
    12. if (up)
    13. removeRows(fromRow + 1, 1, fromParent);
    14. else
    15. removeRows(fromRow, 1, fromParent);
    16. ...
    17. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Switching/moving rows in a model

    Actually what you need to do is remove the second row and insert it before the first one.

    Here is some pseudocode
    Qt Code:
    1. QVariantMap item = secondIndex.itemData();
    2. removeRow(secondIndex.row(), secondIndex.parent());
    3. insertRow(firstIndex.row(), firstIndex.parent());
    4. setItemData(index(firstIndex.row(), 0), item);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2007
    Location
    Augsburg, Germany
    Posts
    75
    Thanks
    4
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Switching/moving rows in a model

    Quote Originally Posted by wysota View Post
    Actually what you need to do is remove the second row and insert it before the first one.

    Here is some pseudocode
    Qt Code:
    1. QVariantMap item = secondIndex.itemData();
    2. removeRow(secondIndex.row(), secondIndex.parent());
    3. insertRow(firstIndex.row(), firstIndex.parent());
    4. setItemData(index(firstIndex.row(), 0), item);
    To copy to clipboard, switch view to plain text mode 
    I'm sure this would work, but if my selection model's currentIndex() points to the secondIndex, the selection would be gone
    I need a solution that does not just work with up/down, it should support exchanging the last with the first item for example..

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Switching/moving rows in a model

    So simply exchange data from both items and emit dataChanged for both of them. Just be aware this is not exchanging rows but exchanging their values. If you have a persistant index pointing to both of them, they will not be exchanged and will be pointing to wrong indexes after the swap. With "my" approach you at least save one of them.

Similar Threads

  1. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  2. QTableView has constant number of rows
    By tomeks in forum Qt Programming
    Replies: 5
    Last Post: 10th December 2008, 15:29
  3. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 16:26
  4. QSqlTableModel inserts empty rows
    By Nesbitt in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2008, 12:47
  5. Removing rows
    By indifference in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2007, 16:54

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.