Search code examples
qtdrag-and-dropqtreewidgettoplevel

QTreeWidget Drag and Drop only in Top Level


I have one QTreewidget which displays a list of objects only in top level. So you can think it as list widget. Because QListWidget doesn't support multi-column, I use QTreeWidget.

I controlled drag-and-drop related properties like this.

dragEnabled  = true

dragDropOverwriteMode = true

dragDropMode = InternalMove

The problem is when I drag and drop items in this treewidget, dropped item goes into child of other item, and this is not what I want. I need only one top level, because what I need is list , not tree.

Is there any way to implement only re-ordering of items? If not, Can you provide me other way instead of QTreeWidget?


Solution

  • Using following code, I could make sortable, multi-column, context-menu triggerable list using QTreeView.

        ui->sensorTreeView->setColumnCount(2);
        ui->sensorTreeView->setSelectionMode(QAbstractItemView::MultiSelection);
        ui->sensorTreeView->setSelectionBehavior(QAbstractItemView::SelectRows);
        ui->sensorTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
        ui->sensorTreeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
        ui->sensorTreeView->setItemsExpandable(false);
        ui->sensorTreeView->setExpandsOnDoubleClick(false);
    
    
        ui->sensorTreeView->setDragEnabled(true);
        ui->sensorTreeView->viewport()->setAcceptDrops(true);
        ui->sensorTreeView->setDropIndicatorShown(true);
        ui->sensorTreeView->setDragDropMode(QAbstractItemView::InternalMove);