[Qt-interest] Drag & Drop of hierachical items in a TreeView
Daniel Price
daniel.price at fxhome.com
Mon Nov 30 11:52:03 CET 2009
-----Original Message-----
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Stephen Kelly
Sent: 27 November 2009 19:19
To: qt-interest at trolltech.com
Subject: Re: [Qt-interest] Drag & Drop of hierachical items in a TreeView
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?
I sent an email to Nokia support and they provided an example - you have to cache the child items recursively using PersistantModelIndexes and then re-constitute the tree after the drop using insertRow() etc. It's a bit messy but it works.
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'm using 4.5.2. Is this a new feature of 4.6? It preserves the children?
>
> 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.
Thanks for the tips.
>
>
> 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.
_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest
No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 9.0.709 / Virus Database: 270.14.87/2535 - Release Date: 11/29/09 19:31:00
More information about the Qt-interest-old
mailing list