Results 1 to 5 of 5

Thread: drag and drop misbehaving

  1. #1
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default drag and drop misbehaving

    I have a model/view system where I am attempting to implement drag and drop. The actual drag and drop seems to be operating correctly... but the model::removeRows() function doesn't appear to be working as documented.

    Here's an example... take the tree describe below

    Qt Code:
    1. rootnode
    2. |
    3. |----A
    4. |----B
    5. |----C
    To copy to clipboard, switch view to plain text mode 
    I can drag C to go under B, but QtGui calls removeRows() with C as the parent, rather than rootnode (invalid QModelIndex).

    the relevant code:
    Qt Code:
    1. bool modelPlugins::removeRows(int row, int count, const QModelIndex & parent)
    2. {
    3. int index(0);
    4. modelPluginItem * parentItem;
    5. if(row < 0 || row + count > rowCount(parent))
    6. return false;
    7. if(parent.isValid())
    8. parentItem = static_cast<modelPluginItem *>(parent.internalPointer());
    9. else
    10. parentItem = rootItem;
    11. beginRemoveRows(parent, row, row + count - 1);
    12. for(; index < count; index++)
    13. parentItem->nondestructiveremovechild(row);
    14. endRemoveRows();
    15. return true;
    16. }
    To copy to clipboard, switch view to plain text mode 
    In my debugger, the modelPluginItems each have a unique id... the root node has ID 0, and C has ID 3... parentItem has ID 3. I can see this if Qt were to attempt to remove all children before removing the item, but it never tries to remove row 2 with an invalid parent index. How can I fix this?
    Life without passion is death in disguise

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

    Default Re: drag and drop misbehaving

    The behaviour is correct. There is a difference between dropping on an item and between items. In the latter case you'll have an invalid root index as the parent in all related methods (dropMimeData, before all).

  3. #3
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: drag and drop misbehaving

    Yes, that's understood... but that has nothing to do with the removeRows() function. As I stated, the drag and drop operation itself is working fine. It's the cleanup (removeRows()) that I'm not understanding correctly.

    When C is moved under B, then removeRows() should be performed on an invalid QModelIndex() with row 2 to remove C. Instead, a removeRows() call is made with C as the parent, which then attempts to remove row 0 under C (which doesn't exist), which returns false (failed operation), and it never attempts to remove C as described at the start of this paragraph.
    Life without passion is death in disguise

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

    Default Re: drag and drop misbehaving

    Could you provide a minimal compilable example reproducing the problem?

  5. #5
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: drag and drop misbehaving

    While producing my minimal example, I spotted the problem on my end.

    I was trying to be efficient by re-assigning the parent of the moving node on the drop operation... can't do that. The drop worked fine (interestingly), but Qt couldn't remove the node properly.

    The tree structure still held a link from the old parent to the data item, but the data item now thought its parent was the parent at the new location. The solution was to do a copy operation rather than a re-parent operation. Both nodes continued to exist until Qt called the removeRows() function, at which point the old node got removed.

    I'm still fairly efficient in that the copy operation involved copying a pointer rather than an object... I just needed to be careful on how the object was removed via the removeRows() function.
    Life without passion is death in disguise

Similar Threads

  1. Drag and Drop
    By MrShahi in forum Qt Programming
    Replies: 1
    Last Post: 10th April 2008, 15:01
  2. Drag and Drop QTableWidget and QTableView
    By scorpion11 in forum Qt Programming
    Replies: 5
    Last Post: 8th April 2008, 09:33
  3. Replies: 7
    Last Post: 8th September 2006, 16:19
  4. Drag and drop outside the application
    By jpn in forum Newbie
    Replies: 7
    Last Post: 27th August 2006, 15:37
  5. Drag and drop revisited
    By Big Duck in forum Newbie
    Replies: 2
    Last Post: 30th June 2006, 16:41

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.