Important: Please read the Qt Code of Conduct -

How to detect the drop target?

  • Hello, I have implemented my own QMimeData class and overriding the drag drop methods for QTreeWidget (in my custom QTreeWidget derived class).

    Drag & Drop works fine - but I want to customize the DropEvent() functionality based on who is the target (if its my widget or some other widget).

    I am getting called retrieveData(), but from that I cannot tell who is calling it (it could be some other application). And I do not get dropEvent() calls (since the drop is not my control). I have no access to Drag object (since I am not the one starting the Drag drop operation).

    How to customize my drop operation in these situations based on who is asking me the drop data?????

  • The clean approach would be to implement the distinction in your widgets - they should know what to do with what kind of data. QMimeData should be used as a transport container only, the actual processing is up to the widgets.

  • Thank you - but the drop targets could be outside my application too. For example, notepad etc...

    When my data is dropped onto notepad (lets say), I get the retrieveData() - but that's all. I do not get the dropEvent() or dropMimeData() etc... where I can check the event position.

    Now, the problem is, for the external targets, I need to have a different dropAction behavior rather than the default one - if I can detect the target, then I can modify the dropAction. But since I am not originating the drag, I cannot subscribe to target change notifications.

    Is there anyway to reliably detect the drop target - atleast to the extent that I should be able to kinow that its not same as my widget and do something about the data source?

  • In the widget that prepares the drag data, you create a [[Doc:QDrag]] object. You eventually call "exec()":/doc/qt-4.8/qdrag.html#exec-2 on that drag. This method has an overload that takes a second parameter, the default action. Set that to be appropriate for the external targets, your internal widgets always can ignore that.

    You might need to reimplement "QAbstractItemView::startDrag() ":/doc/qt-4.8/qabstractitemview.html#startDrag in order to manipulate the drag object.

    I didn't test that recently, but that would be the way I would try first.

Log in to reply