Results 1 to 3 of 3

Thread: QTreeView default drag action

  1. #1
    Join Date
    Nov 2008
    Posts
    33
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTreeView default drag action

    Using QT4.4.3 X11, I have run into the same problem as others regarding the non-standard default drag action of QTreeView. It's documented here: QTreeView drag and drop Bug - defaults to Qt::CopyAction instead of Qt::MoveAction but I can't find any solutions.

    As far as I'm concerned, dragging an item within a tree view should default to moving the item (a la Konqueror, Windows file manager etc.); dragging from another application into the tree view should copy it.

    I've spent a day trying to rectify the problem by implementing dragMoveEvent, dragEnterEvent etc. and setting the drag action explicitly:

    Qt Code:
    1. void QAbstractItemView::dragMoveEvent ( QDragMoveEvent * event ) {
    2. ...
    3. //force a move action unless control key is pressed
    4. if (event->keyboardModifiers() & Qt::ControlModifier)
    5. event->setDropAction(Qt::CopyAction);
    6. else
    7. event->setDropAction(Qt::MoveAction);
    8. event->accept();
    9. event->acceptProposedAction(); //causes cursor icon to change and target item to get highlighted
    10. ...
    11. }
    To copy to clipboard, switch view to plain text mode 

    The above code works fine whilst dragging is taking place (ie: the "+" cursor copy icon disappears) but, by the time model's dropMimeData executes, the dropAction has always reverted to the default CopyAction.
    Setting the treeView's dragDropMode to InternalMove is not a solution as you can not then set the DropAction to Qt::CopyAction if the user presses the control key.

    QUESTION: Where can I fix the QTreeView's default drop action? Is there some kind of compatibility switch at Application level?

  2. #2
    Join Date
    Nov 2008
    Posts
    33
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTreeView default drag action

    Quote Originally Posted by onamatic View Post
    QUESTION: Where can I fix the QTreeView's default drop action? Is there some kind of compatibility switch at Application level?
    To answer my own question (please jump in if I've missed something):

    There doesn't appear to be any "compatibility" switch anywhere.

    The erroneous default dropAction is caused by a mistake in the default implementation of QAbstractItemView::startDrag; override it to get the correct behaviour.

    The example below does most of what the default implementation does but without access to protected members it doesn't emulate the fancy drag icon creation; you just get the standard drag box instead of the item's title. Thanks to another contributor to this forum (whose name I've lost) for most of the code.

    Qt Code:
    1. void QAbstractItemView::startDrag(Qt::DropActions supportedActions)
    2. {
    3. qDebug() << "QAbstractItemView::startDrag; begin";
    4. QModelIndexList indexes = selectedIndexes();
    5. QList<QPersistentModelIndex> persistentIndexes;
    6.  
    7. if (indexes.count() > 0) {
    8. QMimeData *data = model()->mimeData(indexes);
    9. if (!data)
    10. return;
    11. for (int i = 0; i<indexes.count(); i++){
    12. QModelIndex idx = indexes.at(i);
    13. qDebug() << "\tDragged item to delete" << i << " is: \"" << idx.data(NODE_TITLE).toString() << "\"";
    14. qDebug() << "Row is: " << idx.row();
    15. persistentIndexes.append(QPersistentModelIndex(idx));
    16. }
    17.  
    18. QPixmap pixmap = indexes.first().data(Qt::DecorationRole).value<QPixmap>();
    19. QDrag *drag = new QDrag(this);
    20. drag->setPixmap(pixmap);
    21. drag->setMimeData(data);
    22. drag->setHotSpot(QPoint(pixmap.width()/2, pixmap.height()/2));
    23.  
    24. Qt::DropAction defaultDropAction = Qt::IgnoreAction;
    25. if (supportedActions & Qt::MoveAction && dragDropMode() != QAbstractItemView::InternalMove)
    26. defaultDropAction = Qt::MoveAction; //was Qt::CopyAction THIS WAS THE CULPRIT!
    27.  
    28. if ( drag->exec(supportedActions, defaultDropAction) == Qt::MoveAction ){
    29. //when we get here any copying done in dropMimeData has messed up our selected indexes
    30. //that's why we use persistent indexes
    31. for (int i = 0; i<indexes.count(); i++){
    32. QPersistentModelIndex idx = persistentIndexes.at(i);
    33. qDebug() << "\tDragged item to delete" << i << " is: " << idx.data(NODE_TITLE).toString();
    34. qDebug() << "Row is: " << idx.row();
    35. if (idx.isValid()){ //the item is not top level
    36. model()->removeRow(idx.row(), idx.parent());
    37. }
    38. else{
    39. model()->removeRow(idx.row(), QModelIndex());
    40. }
    41. }
    42. }
    43. }
    44. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Smile Re: QTreeView default drag action

    AHA!!!

    This was a positive life saver for me - solved exactly the problem I was facing (and spent all of yesterday trying to solve). I read through ten or so other posts in this forum before getting to this one.

    Thanks a lot!

    I'll just add some more keywords to the thread, to make this information more accessible for people who face the same problem:

    QTreeView, QAbtractItemView, MoveAction, CopyAction

    Amitai.

Similar Threads

  1. Drag and drop in QTreeView
    By Valheru in forum Qt Programming
    Replies: 3
    Last Post: 27th July 2008, 09:36
  2. Drag and Drop on Whitespace of QTreeView
    By T1c4L in forum Qt Programming
    Replies: 6
    Last Post: 14th May 2008, 13:29
  3. Drag & drop items on the same QTreeView
    By wind in forum Qt Programming
    Replies: 2
    Last Post: 11th October 2006, 14:29
  4. drag and drop from QTreeView
    By Untersander in forum Qt Programming
    Replies: 1
    Last Post: 10th April 2006, 09:00
  5. Drag & drop for QTreeView
    By yogeshm02 in forum Qt Programming
    Replies: 2
    Last Post: 30th January 2006, 14:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.