Drag and Drop inside QTableView with QStandardItemModel
Dear ALl,
I have a question that I am hoping someone in the Forum can help me with.
I have QtableView, using the QStandardItemModel. I need to be able to sort/arrange the content of the model by dragging and dropping its items. Is this possible with the QStandarItemModel and QTableView?
so in short I would be able to drag item2 and drop it on top of item1 - result should be that item1 and item2 swap place. I do not want to drop between, only on top of other items. No new lines should be created....
I am configuring the QTableView the following way.
Code:
...
tableView->setContextMenuPolicy(Qt::CustomContextMenu);
tableView->setAlternatingRowColors(true);
tableView->setDropIndicatorShown(true);
tableView->setDragDropOverwriteMode(false);
tableView->setDragEnabled(true);
...
BUt if I drag item1 and drop on item2 nothing happens. If I drag item1 and drop it between item2 and item3. item1 is appended to the model at the very bottom.??
Any help is most appreciated.
Re: Drag and Drop inside QTableView with QStandardItemModel
Quote:
tableView->setDragDropMode(QAbstractItemView:: InternalMove);
tableView->setDragDropMode(QAbstractItemView:: DragDrop);
The second line cancels the first; it isn't an "OR" operation.
Have you read this?
Re: Drag and Drop inside QTableView with QStandardItemModel
Thanks for taking the time to answer me. True with the bottom two lines. I removed the bottom one, and left solely the InternalMove.
I can now drop item1 on item2, but then item1 is cleared (removed) and item2 remains unchanged. What I want is that item1 and item2 just swap place.
I will have a read at you link and see if I can progress with this tomorrow.
cheers!
Re: Drag and Drop inside QTableView with QStandardItemModel
Okay managed to get it working with QTableView and QStandardItems.
here for those who might have similar need:
The key for the success was setting the item.setFlags as below and the three drag options at the bottom.
Code:
// Attach the model to the view
ui->downloadView->setModel(model);
QList<QStandardItem*> data;
itm->setFlags(itm->flags() & ~(Qt::ItemIsDropEnabled));
data.append(itm);
model->appendRow(data);
data.clear();
itm->setFlags(itm->flags() & ~(Qt::ItemIsDropEnabled));
data.append(itm);
model->appendRow(data);
data.clear();
itm->setFlags(itm->flags() & ~(Qt::ItemIsDropEnabled));
data.append(itm);
model->appendRow(data);
data.clear();
itm->setFlags(itm->flags() & ~(Qt::ItemIsDropEnabled));
data.append(itm);
model->appendRow(data);
data.clear();
itm->setFlags(itm->flags() & ~(Qt::ItemIsDropEnabled));
data.append(itm);
model->appendRow(data);
data.clear();
ui->downloadView->setDragDropOverwriteMode(false);
ui->downloadView->setDragEnabled(true);
Re: Drag and Drop inside QTableView with QStandardItemModel
Quote:
QStandardItem* itm = new QStandardItem();
QList<QStandardItem*> data;
itm = new QStandardItem("Breitner");
The first line above is a memory leak because you create the item, do nothing with it, and then overwrite the pointer in the third line.
Thanks for sharing your solution.