PDA

View Full Version : Drag and drop with QTreeWidget



JSPoly
6th February 2007, 22:32
Hi,

I subclassed the code below from QTreeWidget to enable drag and drop. The problem is when i'm draging an item containing childrens, the childrens don't follow the parent on the drop. Is there a problem with mimeData?

Also, is there a way to obtain the index of the parent where I make the drop?

Thanks


void QMyTreeWidget::mouseMoveEvent(QMouseEvent *event)
{
QModelIndexList liste=selectedIndexes();
QMimeData *mimeData = model()->mimeData(liste);
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setHotSpot(event->pos() - rect().topLeft());
Qt::DropAction dropAction = drag->start( Qt::MoveAction);
if (dropAction == Qt::MoveAction) {
model()->removeRows(liste[0].row(),liste.size(),liste[0].parent());
}
}


void QMyTreeWidget::dropEvent(QDropEvent *e)
{
QTreeWidget::dropEvent(e);
e->setDropAction(Qt::MoveAction);
e->accept();
}

jpn
6th February 2007, 22:48
Is there any valid reason for implementing custom drag'n'drop? QTreeWidget supports drag'n'drop out of the box: Using Drag and Drop with Item Views - Using Convenience Views (http://doc.trolltech.com/4.2/model-view-dnd.html#using-convenience-views)

JSPoly
6th February 2007, 23:05
Yes, I want to use the move action without using the atl key. The default drag and drop action is copy.

wysota
6th February 2007, 23:12
Your drop doesn't handle any moving. First you call the base class and then when the event is already handled you change its type.

JSPoly
6th February 2007, 23:18
Ok, so what is the good way to do it? Any example?

wysota
6th February 2007, 23:31
The best way to do it is to move the items yourself, if you have to reimplement the handler... If the default handler doesn't include children then you either need to change the drag to include children or change the drop to include children. Changing the action itself isn't enough, it'll just make the source remove the item it had placed into the drag (without children).