PDA

View Full Version : Moving vs. copying in drag and drop



lana
29th July 2010, 00:08
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!



QApplication app(argc, argv);

QTableWidget table(5, 1);
table.horizontalHeader()->setStretchLastSection(true);
table.setHorizontalHeaderLabels(QStringList() << "Sort Items by Dragging");

table.setSelectionMode(QAbstractItemView::SingleSe lection);
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)
{
QTableWidgetItem * item = new QTableWidgetItem(labels[i]);
item->setFlags(item->flags() & ~Qt::ItemIsDropEnabled);
table.setItem(0, i, item);
}

table.show();
return app.exec();

norobro
29th July 2010, 01:00
Here you go: link (http://doc.trolltech.com/4.6/qabstractitemview.html#DragDropMode-enum)

lana
29th July 2010, 16:39
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.

norobro
29th July 2010, 18:41
I couldn't get it to work either. "InternalMove" works fine on a treewidget which is where I have used it:
#include <QtGui>

int main(int argc, char * argv[]){

QApplication app(argc, argv);

QTreeWidget tree;
tree.setHeaderLabel("Sort Items by Dragging");
tree.setSelectionMode(QAbstractItemView::SingleSel ection);
tree.setDragEnabled(true);
tree.setDropIndicatorShown(true);
tree.viewport()->setAcceptDrops(true);
tree.setDragDropMode(QAbstractItemView::InternalMo ve);
QStringList labels;
labels <<"Item D"<< "Item B"<< "Item C"<< "Item E"<< "Item A";
for (int i=0; i < 5; ++i)
{
QTreeWidgetItem * item = new QTreeWidgetItem();
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 ...

squidge
29th July 2010, 18:58
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.

lana
29th July 2010, 19:41
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.