Results 1 to 5 of 5

Thread: Workaround for StandardItemModel drag drop bug

  1. #1
    Join Date
    Nov 2008
    Posts
    33
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Workaround for StandardItemModel drag drop bug

    I have a QTreeView with a QStandardItemModel implementing a simple "notebook" type of application (ie: a bunch of notes inside a bunch of folders).

    Everything is fine except for internally drag/dropping a folder in the treeview when all the notes in the dragged folder get lost.
    This is a known bug and is detailed here: Drag and drop in QTreeView with QStandardItemModel and InternalMove loses children

    Without this functionality I'm truly stuck, so I'm now subclassing QStandardItemModel
    Qt Code:
    1. class QNotesStandardItemModel : public QStandardItemModel
    To copy to clipboard, switch view to plain text mode 
    and trying to manually sort things out in:
    Qt Code:
    1. bool QNotesStandardItemModel::dropMimeData ( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent ){
    2. QStringList list = data->formats();
    3. if (!data->hasFormat("application/x-qabstractitemmodeldatalist")) return false;
    4. QByteArray encodedData = data->data("application/x-qabstractitemmodeldatalist");
    To copy to clipboard, switch view to plain text mode 

    And now I'm stuck!
    QUESTION 1: encodedData contains a QList<QModelIndex> but how do I get at it? I want a kind of "qobject_cast<QModelIndexList>(encodedData)" type of thing! Just can't see how though.

    QUESTION 2: Is there an easy way of working round the "lost children" problem? I really wanted to keep this application as simple as possible and not have to subclass standard classes which generally work fine out of the box.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Workaround for StandardItemModel drag drop bug

    Re Q1:
    If you use drag-n-drop between two different applications, the QModelIndexes will not be of much use to you.
    If you drag within a single application, just create a subclass of QDrag or QMimeData and add a QList<QModelIndex> to it. Then (at the drop site) cast the object back to your class and access the QModelIndexes. (No need to go through encode/decode troubles within a single application.)

    HTH

  3. #3
    Join Date
    Nov 2008
    Posts
    33
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Workaround for StandardItemModel drag drop bug

    Thanks for helping me Caduel!

    Quote Originally Posted by caduel View Post
    Re Q1:
    If you use drag-n-drop between two different applications, the QModelIndexes will not be of much use to you.
    Understood, I'm only drag-n-dropping inside a single treeview so that's fine.

    ...Then (at the drop site) cast the object back to your class and access the QModelIndexes. (No need to go through encode/decode troubles within a single application.)
    There's my problem - I can't see how to cast from the QByteArray returned by the call to mimeDataPtr->data() to the "application/x-qabstractitemmodeldatalist" that I want. I'm missing something conceptually here I think!

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Workaround for StandardItemModel drag drop bug

    I was suggesting to not use that QByteArray at all.
    Qt Code:
    1. class MyMimeData : public QMimeData
    2. {
    3. QModelIndexList indexes;
    4. public:
    5. MyMimeData(const QModelIndexList & indexes)
    6. : indexes_(indexes)
    7. {}
    8. const QModelIndexList & indexes() const { return indexes_; }
    9. };
    10.  
    11. QMimeData * QNotesStandardItemModel::mimeData(const QModelIndexList &indexes) const
    12. {
    13. // maybe set a mimetype...
    14. // but finally
    15. return new MyMimeData(indexes);
    16. }
    17.  
    18. bool QNotesStandardItemModel::dropMimeData ( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent )
    19. {
    20. if (MyMimeData *mmd = qobject_cast<MyMimeData>(data))
    21. {
    22. // access dragges indexes as: mmd->indexes()
    23. // ...
    24. return true;
    25. }
    26. return false;
    27. }
    To copy to clipboard, switch view to plain text mode 
    HTH

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

    onamatic (9th November 2008)

  6. #5
    Join Date
    Nov 2008
    Posts
    33
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Workaround for StandardItemModel drag drop bug

    That works brilliantly. Thank you very much Caduel for taking the time to write my code for me! Much appreciated.

    Bob

Similar Threads

  1. Drag & Drop when parent and childs accept drops?
    By gri in forum Qt Programming
    Replies: 0
    Last Post: 17th October 2008, 09:00
  2. Strange behavior with Drag and Drop (not always drops)
    By Mona Shokrof in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2007, 18:00
  3. Drag and Drop
    By allensr in forum Qt Programming
    Replies: 1
    Last Post: 11th December 2006, 20:50
  4. Replies: 7
    Last Post: 8th September 2006, 16:19
  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.