[Qt-interest] Drag & Drop of hierachical items in a TreeView
Stephen Kelly
steveire at gmail.com
Fri Nov 27 20:19:01 CET 2009
Daniel Price wrote:
> I'm trying to figure how to do drag and drop editing of hierarchical data
> within an QAbstractItemModel-derived adaptor within a QTreeView.
>
> Single items with no children seem to work fine (I'm only interested in
> moving data, not copying it). However if I move an item with children, the
> children are lost and I'm not sure how I can preserve them as the
> dropMimeData() virtual only gives me the mime data and drop target.
It is odd that the children are lost. How are you doing the actual move?
If you are using Qt4.6, try the beginMoveRows/endMoveRows api. It should not
be necessary to reconstruct the entire tree if you do that.
>
> I can think of same ways around this but I don't know what the 'best
> practice' solution would be:
>
> 1) Encode the QModelIndex of the original item into the mime data
> (QDataStream) and use it to recursively iterate through and reconstruct
> the tree in the drop location (it seems that the source items are not
> removed until after dropMimeData() is called so the index should remain
> valid).
>
> 2) Cache the source model index somewhere when a drag is initiated
> (mimeData()?) and do the above.
>
> Any suggestions? Has anyone got this working?
The way we do this in akonadi is by encoding a url of the item in the
mimedata. The url can be used to get the internal data object (Collection or
Item), and a move operation is kicked off. The move in the model is done
when we get notified that the move is complete.
see dropMimeData here:
http://websvn.kde.org/trunk/KDE/kdepimlibs/akonadi/entitytreemodel.cpp?view=markup
and monitoredCollectionMoved here:
http://websvn.kde.org/trunk/KDE/kdepimlibs/akonadi/entitytreemodel_p.cpp?view=markup
All the best,
Steve.
>
>
> Daniel Price
> Software Engineer
> FXhome Limited
>
> ________________________________
> This email is confidential. It may also be privileged or otherwise
> protected by work product immunity or other legal rules. If you are not
> the intended recipient please notify the sender. Please delete the message
> from all places in your computer where it is stored. You should not copy
> the email or use it for any purpose or disclose its contents to any other
> person.To do so may be unlawful. Email is an informal means of
> communicating and may be subject to data corruption accidentally or
> deliberately. For this reason it is inappropriate to rely on advice
> contained in an email without obtaining written confirmation of it first.
>
> FXhome Limited is a limited company registered in England and Wales.
> Registered number: 04172812. Registered office: The Henderson Business
> Centre, Ivy Road, Norwich, Norfolk, NR5 8BF, U.K.
More information about the Qt-interest-old
mailing list