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?
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?
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,
_
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?
Can you show the code that you use to put the model indexes into the mime data?
Cheers,
_
QMimeData *proxyModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData *mimeData = QSortFilterProxyModel::mimeData(indexes);
return mimeData;
}
this where i am adding qmodel index
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,
_
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
That looks good, what kind of problem do you have?
Cheers,
_
Might be silly but from this implementation, i am not getting parent , or can i get?
That's true.
The base implementation seems to pass the parent to its decode function, probably the item the drop is on.
Cheers,
_
How can achieve drag and drop for tree than?
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,
_
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?
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,
_
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.
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,
_
Bookmarks