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

    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?

  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

    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.