Important: Please read the Qt Code of Conduct -

QTreeView internal drag'n'drop

  • I have custom subclasses of a QTreeView and a QAbstractItemModel. So far everything is working. What I would like to add is the option that the user can change the parent of an item in the tree view by dragging it around. I followed the corresponding documentation and extended my classes:




    Qt::DropActions TaskWidgetTreeModel::supportedDropActions() const
        return Qt::MoveAction;
    Qt::ItemFlags TaskWidgetTreeModel::flags(const QModelIndex& index) const
        if (!index.isValid()) {
            return 0;
        // Default flags
        Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
        // Add item specific flags
        if (index.isValid())
            return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | flags;
            return Qt::ItemIsDropEnabled | flags;
        return flags;

    After those modifications I am able to drag an item around in the tree using the mouse. However, the changes are not applied. This is not surprising to me. However, the documentation mentions that the QAbstractItemModel provides sane implementations for the required drag(), drop() and mineData() methods so that implementing those is usually not necessary. It also mentions that the model must support removeRow() and all that stuff which is implemented in my model and works well for everything that is not related to drag'n'drop.

    Can anybody tell me what else I need to do in order to be able to actually process the drag'n'droping?

  • Bump... anyone? :(

  • Hi @Joel-Bodenmann
    Have you got any solution/feedback for your question?

Log in to reply