Nominate our 2022 Qt Champions!

How to allow only QAbstractView::AboveItem and BelowItem dragevents in a QTreeWidgetclass and get the source position and destination position

  • I'm currently using a custom qt widget class to accomplish this so far. I've been able to use the following code in order to restrict above and below events only, (reorder) and avoid moving items to top level.

    void CustomTreeWidget::dropEvent(QDropEvent *event) {
        QModelIndex index = indexAt(event->pos());
        QTreeWidgetItem* item = itemAt(event->pos());
        DropIndicatorPosition position = dropIndicatorPosition();
        if( !index.isValid() )
        if( position != QAbstractItemView::AboveItem && position != QAbstractItemView::BelowItem ){

    But I'm not sure how to extract both source and destination position. It isn't even very clear what event-pos() refers to in every context. event->source() gives a QObject, but that isn't comparable to QTreeWidgetItems, and I'm not sure how to actually extract the widgetitem or position from the source.

Log in to reply