'QTreeWidget sub classing, stop drop indicator from showing given unwanted dropIndicatorPosition

I have the following problem. I need to stop the drop indicator from showing up when I don't want to complete the drop event, which is conditional on dropIndicatorPosition() returning either QAbstractItemView::BelowItem or QAbstractItemView::AboveItem. Right now I subclass QTreeWidget and have the following implementation of dropEvent.

void SubClassedQTreeWidget::dropEvent(QDropEvent *event) {
    DropIndicatorPosition position = dropIndicatorPosition();
    if( position != QAbstractItemView::BelowItem && position != QAbstractItemView::AboveItem ){
        // exit since we don't care for other types of drop events
        return;
    }
    QTreeWidget::dropEvent(event);
}

I wanted to do this in order to stop visual indication that any drop can happen from showing up. I only want inbetween black lines to show up. I've tried re-implementing dragMoveEvent, however setting the drop indicator dynamically via:

DropIndicatorPosition position = dropIndicatorPosition();
setDropIndicatorShown(!(position != QAbstractItemView::BelowItem && position != QAbstractItemView::AboveItem));

doesn't actually do anything, and I would expect some way to not have to perform logic at every drag any way.



Solution 1:[1]

You should override QAbstractItemModel::flags and add Qt::ItemIsDropEnabled flag to the items that can be used as a drop target.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 olya