Moving vs. copying in drag and drop
I'm trying to learn how to do drag and drop. I wrote a simple program which is supposed to permit the user to sort items in a table. It almost does what I want, but when the user drops items at new locations, copies are made. I can't figure out how to change it so that the original item is deleted in addition to the copy being made. Can anyone offer me some assistance? Thanks!
Code:
table.horizontalHeader()->setStretchLastSection(true);
table.
setHorizontalHeaderLabels(QStringList() <<
"Sort Items by Dragging");
table.setDragEnabled(true);
table.setAcceptDrops(true);
table.setDropIndicatorShown(true);
const char * labels[] = { "Item D", "Item B", "Item C", "Item E", "Item A" };
for (int i=0; i < 5; ++i)
{
item->setFlags(item->flags() & ~Qt::ItemIsDropEnabled);
table.setItem(0, i, item);
}
table.show();
return app.exec();
Re: Moving vs. copying in drag and drop
Re: Moving vs. copying in drag and drop
Thanks for the reply. I tried setting the dragDropMode to QAbstractItemView::InternalMove but that also is not quite what I want.
In the default state, copies of the item are created but the original is left behind.
With InternalMove set, the item moves but an empty slot is left behind.
I would like the item to move but no empty slot to be left behind. None of the other drag/drop related options I saw seemed to do just this, either.
Re: Moving vs. copying in drag and drop
I couldn't get it to work either. "InternalMove" works fine on a treewidget which is where I have used it:
Code:
#include <QtGui>
int main(int argc, char * argv[]){
tree.setHeaderLabel("Sort Items by Dragging");
tree.setDragEnabled(true);
tree.setDropIndicatorShown(true);
tree.viewport()->setAcceptDrops(true);
labels <<"Item D"<< "Item B"<< "Item C"<< "Item E"<< "Item A";
for (int i=0; i < 5; ++i)
{
item->setText(0,labels[i]);
item->setFlags(item->flags() & ~Qt::ItemIsDropEnabled);
tree.insertTopLevelItem(i,item);
}
tree.show();
return app.exec();
}
Probably not the look you want though ...
Re: Moving vs. copying in drag and drop
You could allow the object to be moved and then when the object has been dropped remove the empty slot yourself. If you have created an appropriate model and data store for the objects, then this should be very easy to do and just require an update to be sent to the control.
Re: Moving vs. copying in drag and drop
Norobro, the Tree version works for me too. I was thinking Table but maybe a Tree view would be a natural model also. I will think about it. Thanks.
Fatjuicymole, I took your suggestion and turned my Table into a derived class, then overrode the dropMimeData method so it called the base version then cleaned up the extra row. That makes the Table version work as desired. Thanks.