[Qt-interest] Drag and drop model/view questions

Stephen Kelly steveire at gmail.com
Sun Jan 31 12:02:54 CET 2010


Jeremy Sanders wrote:

> I've managed to get basic dragging and dropping working in my tree model.
> I have a few questions:
> 
> 1. If I have CopyAction and MoveAction as supported drop operations, how
> does the user change between moving and copying? Is this documented?
> 
> I'd like to having moving by default, but copying only if Ctrl is held
> down. Is this possible?

I don't know if the way to do this is standardized.

The way we do it in Akonadi is to reimplement a few methods in QTreeView, 
present the user with a popup menu of options or handle modifier keys if 
pressed.

See DragDropManager::processDropEvent( QDropEvent *event ) called from 
EntityTreeView::dropEvent( QDropEvent * event ):

http://websvn.kde.org/trunk/KDE/kdepimlibs/akonadi/dragdropmanager.cpp?view=markup
http://websvn.kde.org/trunk/KDE/kdepimlibs/akonadi/entitytreeview.cpp?view=markup

> 
> 2. Is it possible to get moving implemented in a single method? As moving
> is done by dropping then deleting, my application sees it as two separate
> operations. This means it shows up as two operations in the undo menu. I'd
> like the user to be able to undo both operations in one go.

If using Qt 4.6, use QAbstractItemModel::{beginMoveRows,endMoveRows}. It's 
possible to do it pre 4.6 using layoutAboutToBeChanged/layoutChanged, but 
very tricky to get right.

> 
> The current delete then paste way of moving also means that the naming of
> the items in my tree gets messed up. Each item in my model needs a unique
> name. As a moved item is pasted before deletion, I have to give the item a
> new name to preserve uniqueness, because I don't know a deletion is going
> to occur. This is pretty ugly.

Yes, this is what the move rows api makes easier.

> 
> 3. Is it possible for drag and drop to check the target is a possible
> destination so that the mouse cursor can be changed? It would be good if
> the user could get immediate feedback that the destination is valid.

Again, you could reimplement some stuff in the view to query the models 
data() method and determine if the drop will likely succeed and change the 
mouse cursor. Unfortunately there is no virtual model method similar to bool 
QAIM::canDrop(QMimeData *, ...). I think there's a Qt Bug for it though for 
Qt 5 whenever that appears.

Hope that helps,

Steve.

> 
> Thanks
> 
> Jeremy
> 
> 



More information about the Qt-interest-old mailing list