Results 1 to 14 of 14

Thread: Moving items in QAbstractItemModel

  1. #1
    Join Date
    Mar 2009
    Location
    Belchatow, Poland
    Posts
    38
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Moving items in QAbstractItemModel

    I made a treeview with a model based on this example http://doc.qt.nokia.com/latest/itemv...treemodel.html And now I want to move items from one branch to another. Here's some of my code:

    Qt Code:
    1. QModelIndexList list = selectedIndexes();
    2. RosterSortProxy *proxy = dynamic_cast<RosterSortProxy*>(model());
    3. RosterItemModel *itemModel = dynamic_cast<RosterItemModel*>(proxy->sourceModel());
    4.  
    5. QString group = action->property("group").toString();
    6.  
    7. foreach(QModelIndex in, list) {
    8. QModelIndex index = proxy->mapToSource(in);
    9.  
    10. if(index.isValid()) {
    11. RosterContact *cnt = static_cast<RosterContact*>(index.internalPointer());
    12. if(cnt) {
    13. itemModel->moveToGroup(cnt, group);
    14. }
    15. }
    16. }
    17.  
    18. proxy->invalidate();
    To copy to clipboard, switch view to plain text mode 

    moveToGroup:
    Qt Code:
    1. RosterItem *newGroup = groupItem(groupName);
    2. RosterItem *oldGroup = groupItem(item->contact()->group());
    3.  
    4. if(newGroup != oldGroup) {
    5. oldGroup->removeChild(item);
    6. newGroup->appendChild(item);
    7.  
    8. if((oldGroup->childCount() == 0) && (oldGroup != m_root)) {
    9. removeGroup(oldGroup);
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    removeChild and appendChild just remove/append the item to group's list of children.

    And the problem I'm having is sometimes when I try to move a item, the program crashes, I tried to find a pattern for those crashes but couldn't. Also sometimes not all selected items are moved. I think it's an issue with the model not being updated, but I tried layoutAboutToBeChanged() and beginMoveRows() I even tried using both beginRemoveRows() and beginInsertRows() with no luck, I don't know if I'm using them wrong or what.

    Please help.

  2. #2
    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: Moving items in QAbstractItemModel

    What is the content of removeChild() and appendChild()?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2009
    Location
    Belchatow, Poland
    Posts
    38
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Moving items in QAbstractItemModel

    Roster Item is the equivalent of TreeItem in the example, appendChild is already there I just added removeChild.

    appendChild:
    Qt Code:
    1. m_children.append(item);
    To copy to clipboard, switch view to plain text mode 

    removeChild:
    Qt Code:
    1. m_children.removeAll(item);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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: Moving items in QAbstractItemModel

    That's wrong. The model from the example was never fit to be fed live. When you manipulate the underlying data structure, you always have to call begin{Insert,Remove,Move}Rows() and end{Insert,Remove,Move}Rows().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2009
    Location
    Belchatow, Poland
    Posts
    38
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Moving items in QAbstractItemModel

    But should I do:
    beginMoveRows
    removeChild
    appendChild
    endMoveRows

    or

    beginRemoveRows
    removeChild
    endRemoveRows

    beginInsertRows
    appendChild
    endRemoveRows

    Cause I tried both and still the program is crashing. Maybe I should call those functions somewhere earlier in the code?

    //edit:
    Ok I think I got it, I didn't change the Item's parent, so it thought it had other parent then it really had.

    One other thing, when I select multiple items in my tree view, not all of them are moved, usually one or two stay in the same group. What could be the reason for that?
    Last edited by arturo182; 31st May 2011 at 08:29.

  6. #6
    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: Moving items in QAbstractItemModel

    My guess is that relative row numbers change when you start removing items and you are not taking that into account in your foreach loop.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Mar 2009
    Location
    Belchatow, Poland
    Posts
    38
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Moving items in QAbstractItemModel

    Nope, I was wrong, the problem still exists. I can only move each item once, when I try to move it the second time it gets cloned (one copy stays in old group, and second is created in new group) on third move it crashes.

    I still can't find what I'm doing wrong, I'm removing the item from old group, append it to new group, change item's parent. What else do I have to do. Can some one provide me with an example please?

  8. #8
    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: Moving items in QAbstractItemModel

    Show your current code, please.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Mar 2009
    Location
    Belchatow, Poland
    Posts
    38
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Moving items in QAbstractItemModel

    The only change I made is to appendChild function, it now looks like this:
    Qt Code:
    1. void RosterItem::appendChild(RosterItem *item)
    2. {
    3. item->m_parent = this;
    4. m_children.append(item);
    5. }
    To copy to clipboard, switch view to plain text mode 

    All the previous code is unchanged. I tried adding beginMoveRows but it caused a crash, I tried to use it like so:
    Qt Code:
    1. beginMoveRows(oldGroup->index(), item->row(), item->row(), newGroup->index(), newGroup->rowCount());
    2. oldGroup->removeChild(item);
    3. newGroup->appendChild(item);
    4. endMoveRows();
    To copy to clipboard, switch view to plain text mode 

    Maybe I should inherit QStandardItem or it's used for something else?

  10. #10
    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: Moving items in QAbstractItemModel

    What does beginMoveRows() return?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Mar 2009
    Location
    Belchatow, Poland
    Posts
    38
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Moving items in QAbstractItemModel

    I tried moving same item three times, the first time it returned true, the second time it returned true but a debug message was displayed stating "endMoveRows: Invalid index" and the third time it returned false and crashed.

    I'm sure you're right, I'm not reindexing rows' numbers correctly but I don't know how to fix it.

  12. #12
    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: Moving items in QAbstractItemModel

    I don't like the idea of diving beneath the proxy level and extracting the internal pointer outside the model. In my opinion you should ignore the fact that the underlying model deals with some groups and contacts and you should focus solely on the top level of your model stack. Otherwise you should somehow resort the index list you get so that you can then traverse the list in a proper order that won't break your indexes. Remember that each change in the model invalidates all model indexes. You should first map all the indexes to items and then when you have them all, start moving them. It'd be best if you embedded all the logic directly in the model.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. The following user says thank you to wysota for this useful post:

    arturo182 (31st May 2011)

  14. #13
    Join Date
    Mar 2009
    Location
    Belchatow, Poland
    Posts
    38
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Moving items in QAbstractItemModel

    I just tried this:
    Qt Code:
    1. QList<RosterContact*> contacts;
    2. foreach(QModelIndex in, list) {
    3. QModelIndex index = proxy->mapToSource(in);
    4.  
    5. if(index.isValid()) {
    6. if(index.data(RosterItem::TypeRole) == RosterItem::Contact) {
    7. RosterContact *cnt = static_cast<RosterContact*>(index.internalPointer());
    8. if(cnt) {
    9. contacts.append(cnt);
    10. }
    11. }
    12. }
    13. }
    14.  
    15. foreach(RosterContact *cnt, contacts) {
    16. itemModel->moveToGroup(cnt, group);
    17. }
    To copy to clipboard, switch view to plain text mode 

    And it works great, no crashes and all items are moved. I know looping through contacts twice is not very efficient but I guess I don't have much choice.
    What do you think of this solution?

  15. #14
    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: Moving items in QAbstractItemModel

    It's certainly better than getting crashes. But you should really change wrap it all into your model API as using internalPointer() outside the model is just looking for trouble.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Moving Items with itemChange?
    By konvex in forum Qt Programming
    Replies: 5
    Last Post: 21st November 2008, 14:36
  2. QAbstractItemModel bold items
    By SiLiZiUMM in forum Qt Programming
    Replies: 1
    Last Post: 21st April 2008, 19:48
  3. Moving items within a model
    By iswm in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2007, 08:29
  4. Moving items between two views.
    By YuriyRusinov in forum Qt Programming
    Replies: 11
    Last Post: 2nd April 2007, 13:53
  5. Problem inserting child items into a QAbstractItemModel
    By Valheru in forum Qt Programming
    Replies: 5
    Last Post: 14th October 2006, 18:35

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.