Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTreeWidget Drag and Drop InternalMove problems



  • I am using a QTreeWidget with Drag and Drop operations and dragDropMode set to InternalMove.
    For the most part, this works as expected, with one exception:

    1. I want to allow dropping only for certain items, depending on the dragged item. I have overridden dragMoveEvent to only accept events where my constraints hold. This works, except for one constraint: I do not want it to be possible for an item to be dragged to root level. I do not know how to achieve this.
      For my other constraints, I use itemAt(event.pos()) to get the target item. However, when I drag to a position just above a root level item (in the GUI, the root item is no longer marked - instead there is a horizontal line above it), this also gives me the root item, so I can't use it to decide whether I will be dragging to root level or into a root item.

    This is supposed to be allowed and works fine:
    !http://s16.postimage.org/ckt4nx491/dragright.png(Drop should be allowed)!

    This is supposed to be forbidden, but I don't see a way to distinguish it from the first case:
    !http://s16.postimage.org/v5hcuzpol/dragwrong.png(Drop should not be allowed)!



  • You don't need to override dragMoveEvent to do this. When creating the items, use setFlags() to set Qt::ItemIsDragEnabled for items which can be dragged and Qt::ItemIsDropEnabled for items which can be targets of a drag.

    To prevent the widget from accepting drags to the root level, access the root item with invisibleRootItem() and clear the "drop enabled" flag on it.


Log in to reply