Results 1 to 14 of 14

Thread: Moving items in QAbstractItemModel

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.


  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

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.


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

    arturo182 (31st May 2011)

  6. #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

    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?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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
  •  
Qt is a trademark of The Qt Company.