Results 1 to 16 of 16

Thread: How to get Qmodelindex from mimedata in drop event for tree?

  1. #1
    Join Date
    Oct 2015
    Posts
    18
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default How to get Qmodelindex from mimedata in drop event for tree?

    I have mimedata which i need to be caught in dropevent and i am able to do that, but i want to know about can't i get QModelIndex from mimedata in dropevent ?

    As i am have tree view i need to make parent relation. is there anyother way?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get Qmodelindex from mimedata in drop event for tree?

    QModelndex instances are only safely valid as long as there is no operation on the model or event processing happens.
    Since the latter is true during drag&drop, the model index could have become invalid.

    That might not be the case for your model though, it is just not safe under generic assumptions.

    One option would be to store the QModelIndex in a QPersistantModelIndex member and then access that member on drop.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2015
    Posts
    18
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: How to get Qmodelindex from mimedata in drop event for tree?

    Agreed.. Can you tell me one thing....

    For drag and drop i implementing dropevent in treeview and here i need qmodelindexes. For this in treemodel i have mimdata method from where i am binding qmodelindexlist into mimedata.

    Now in dropevent i catch this mimedata,but not able to figure out how to get qmodelindexes from it.

    Can you please tell me the way to do this?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get Qmodelindex from mimedata in drop event for tree?

    Can you show the code that you use to put the model indexes into the mime data?

    Cheers,
    _

  5. #5
    Join Date
    Oct 2015
    Posts
    18
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: How to get Qmodelindex from mimedata in drop event for tree?

    QMimeData *proxyModel::mimeData(const QModelIndexList &indexes) const
    {
    QMimeData *mimeData = QSortFilterProxyModel::mimeData(indexes);
    return mimeData;
    }






    this where i am adding qmodel index

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get Qmodelindex from mimedata in drop event for tree?

    Ah.

    This is what gets called:
    http://code.woboq.org/qt5/qtbase/src...11QModelIndexE
    which calls
    http://code.woboq.org/qt5/qtbase/src...R11QDataStream

    So you would need code like in decodeData (right below in there) to retrieve the model data from the mime data's byte array.

    Cheers,
    _

  7. #7
    Join Date
    Oct 2015
    Posts
    18
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: How to get Qmodelindex from mimedata in drop event for tree?

    Sorry, but i didnt get you exactly? Can you please explain.

    const QMimeData *mimeData = event->mimeData();
    QByteArray encoded = mimeData->data("application/x-qabstractitemmodeldatalist");
    QDataStream stream(&encoded, QIODevice::ReadOnly);
    QMap<int, QVariant> roleDataMap;
    while (!stream.atEnd())
    {}




    this is my drop event ? can you tell me whats wrong here, please

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get Qmodelindex from mimedata in drop event for tree?

    That looks good, what kind of problem do you have?

    Cheers,
    _

  9. #9
    Join Date
    Oct 2015
    Posts
    18
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: How to get Qmodelindex from mimedata in drop event for tree?

    Might be silly but from this implementation, i am not getting parent , or can i get?

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get Qmodelindex from mimedata in drop event for tree?

    That's true.

    The base implementation seems to pass the parent to its decode function, probably the item the drop is on.

    Cheers,
    _

  11. #11
    Join Date
    Oct 2015
    Posts
    18
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: How to get Qmodelindex from mimedata in drop event for tree?

    How can achieve drag and drop for tree than?

  12. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get Qmodelindex from mimedata in drop event for tree?

    As I said earlier, you could store the dragged index in a persistant model index member and access that on drop.
    Or you encode the drag object in the mime data such that you can get all the necessary information back.

    Cheers,
    _

  13. #13
    Join Date
    Oct 2015
    Posts
    18
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: How to get Qmodelindex from mimedata in drop event for tree?

    I also thought about same points .... sure i will try and keep posted about it.

    For second point i think i need to do recursion, right?

  14. #14
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get Qmodelindex from mimedata in drop event for tree?

    That depends on your tree/data.

    If it can't change due to outside events during drag&drop, then you only need to know the "root" of the subtree you are dragging.

    Cheers,
    _

  15. #15
    Join Date
    Oct 2015
    Posts
    18
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: How to get Qmodelindex from mimedata in drop event for tree?

    Ya true, but what if i need only those child which are dragged, i need to maintain some kind of lit or vector of its id's right? for this i need to do recursion.

  16. #16
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get Qmodelindex from mimedata in drop event for tree?

    If that is what you need then that is what you need.

    E.g. if you drag a directory in a file system tree, the only thing you would need to know is the path of that directory, not for any of its children.

    But if your data requires you to know the full subtree, then that is what you need to have access to.

    Cheers,
    _

Similar Threads

  1. Event Filter doesnt work on Drop Event
    By DNW in forum Qt Programming
    Replies: 3
    Last Post: 25th October 2015, 07:55
  2. drag and drop of tree item
    By sajis997 in forum Qt Programming
    Replies: 0
    Last Post: 14th December 2011, 23:33
  3. drag and drop in only one hierarchy of the tree widget
    By fulbay in forum Qt Programming
    Replies: 0
    Last Post: 17th December 2010, 16:57
  4. Drag and Drop for tree model
    By frank100 in forum Qt Programming
    Replies: 5
    Last Post: 8th December 2010, 20:21
  5. Drag and Drop MimeData
    By Zephro in forum Qt Programming
    Replies: 10
    Last Post: 16th May 2006, 20:20

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.