Qt World Summit: Register Today!

[SOLVED] How to vary drag and drop flags?

  • My application has a widget with a QTreeView in it that I want to use drag and drop in. The data being dragged comes from either 2 different external widgets, or, I also want to be able to do internal drag and drops to rearrange the data, if necessary.

    I am using a model I derived from QStandardItemModel inside the QTreeView, and I set the Qt::ItemIsDragEnabled and/or Qt::ItemIsDropEnabled for each item in the (overridden) QStandardItemModel::flags (const QModelIndex &index) method.

    My problem is that I would like to set different flags depending on where the current drag is coming from, and where it's going to.

    For example, if my drag is coming from one of the external widgets, I want to only allow drops on certain nodes in the tree. If it comes from the other external widget, I want to allow drops on DIFFERENT nodes. Further, if I am doing an internal move, I want to allow different nodes yet again.

    I tried setting the flags to allow drag and drop for everything, and then try to determine if it was "legal" in myModel::dropMimeData(...) method, and that works, however, if I DO find it illegal and return false from this method (i.e. don't do the proposed action), it seems like it's too late. For example, if I am trying to do an internal MOVE of the data from one place to another, and I return false from this method, it will not do the DROP, but it still deletes the data from the SOURCE of the drag (because it was a MoveAction, I assume). Obviously that's no good.

    Does anybody have any ideas how I can accomplish this?

    Thanks in advance for any suggestions!

  • I had a similar need. I ended up in reimplementing the dragEnter, dragMove and dragLeave events in the tree view (by subclassing). That methods know the sender of the drag and can allow or deny the drop. I'm not sure whether there's another method. There are no "callbacks" or whatsoever to "ask" if a drag/drop is ok in the current state of the application.

  • Volker.
    I did try overriding dragEnter in one of my attempts to solve this, however, it doesn't get called at all when you are doing an internal drag/drop. I didn't override the dragMove as well, though. Perhaps that DOES get called on internal moves and I just didn't take that far enough.

    I will give that a try (when I get back in the office on Monday) and let you know how it goes.

    Thanks for your time Volker, it's appreciated!

  • Oh, I just forgot to mention another method: startDrag(). So this is what I have reimplemented in my tree view:

    • "QAbstractItemView::startDrag() ":/doc/qt-4.8/qabstractitemview.html#startDrag
    • "QAbstractItemView::dragEnterEvent() ":/doc/qt-4.8/qabstractitemview.html#dragEnterEvent
    • "QAbstractItemView::dragMoveEvent() ":/doc/qt-4.8/qabstractitemview.html#dragMoveEvent
    • "QAbstractItemView::dragLeaveEvent() ":/doc/qt-4.8/qabstractitemview.html#dragLeaveEvent
    • "QAbstractItemView::dropEvent() ":/doc/qt-4.8/qabstractitemview.html#dropEvent

Log in to reply