PDA

View Full Version : How to get Qmodelindex from mimedata in drop event for tree?



dikku
15th February 2016, 11:23
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?

anda_skoa
15th February 2016, 13:01
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,
_

dikku
16th February 2016, 09:30
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?

anda_skoa
16th February 2016, 10:08
Can you show the code that you use to put the model indexes into the mime data?

Cheers,
_

dikku
16th February 2016, 10:12
QMimeData *proxyModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData *mimeData = QSortFilterProxyModel::mimeData(indexes);
return mimeData;
}






this where i am adding qmodel index

anda_skoa
16th February 2016, 10:24
Ah.

This is what gets called:
http://code.woboq.org/qt5/qtbase/src/corelib/itemmodels/qabstractitemmodel.cpp.html#_ZNK18QAbstractItemMod el8mimeDataERK5QListI11QModelIndexE
which calls
http://code.woboq.org/qt5/qtbase/src/corelib/itemmodels/qabstractitemmodel.cpp.html#_ZNK18QAbstractItemMod el10encodeDataERK5QListI11QModelIndexER11QDataStre am

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,
_

dikku
16th February 2016, 10:53
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

anda_skoa
16th February 2016, 11:03
That looks good, what kind of problem do you have?

Cheers,
_

dikku
16th February 2016, 11:14
Might be silly but from this implementation, i am not getting parent , or can i get?

anda_skoa
16th February 2016, 11:48
That's true.

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

Cheers,
_

dikku
17th February 2016, 10:36
How can achieve drag and drop for tree than?

anda_skoa
17th February 2016, 12:46
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,
_

dikku
17th February 2016, 15:53
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?

anda_skoa
17th February 2016, 16:14
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,
_

dikku
18th February 2016, 03:32
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.

anda_skoa
18th February 2016, 09:19
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,
_