PDA

View Full Version : Drag & drop in QTreeWidget



Maluna34
27th June 2017, 11:09
Hello ! :)
(Sorry for my english)

I am currently trying to make a drag & drop in a QTreeWidget. So I put the corresponding settings and the method dropEvent :



class TreeWidget : public QTreeWidget
{
protected:

virtual void dropEvent(QDropEvent *event) override
{
QModelIndex index = indexAt(event->pos());
if (!index.isValid()) { // just in case
event->setDropAction(Qt::IgnoreAction);
return;
}

QTreeWidgetItem* item = itemFromIndex(index);
qDebug() << "drop on item" << item->text(0);

QTreeWidget::dropEvent(event);
}
};

int main()
{
TreeWidget *listWidget = new TreeWidget;
listWidget->setSelectionMode(QAbstractItemView::SingleSelectio n);
listWidget->setDragEnabled(true);
listWidget->viewport()->setAcceptDrops(true);
listWidget->setDropIndicatorShown(true);
listWidget->setDragDropMode(QAbstractItemView::InternalMove);
}


But in my case, I would like to move only parent items. In the code, I get the destination item, but how get dragged item ?

Have I to overload a drag method ? Launch the drag myself from mousePressEvent ? What is the best way to do ?

Thanks !