Custom QTableView and model: InternalMove also causes insert/delete
-
Hello,
I have a custom QTableView and a custom model derived from QAbstractTableModel. I only want to allow for rows to be dragged and dropped within the tableview (basically only re-ordering of the rows is allowed).I am applying the following settings:
qTableView->setDropIndicatorShown(true); qTableView->setDragEnabled(true); qTableView->viewport()->setAcceptDrops(true); qTableView->setDragDropMode(QAbstractItemView::InternalMove); qTableView->setDefaultDropAction(Qt::MoveAction); qTableView->setDragDropOverwriteMode(false);
I also have a custom Row object with 10 attributes, and only 5 of them are shown in the tableview.
When I drag/drop, I see that a new row is inserted at the new location, the attributes visible in tableview are copied over to the new row, and the row at the old location is deleted.
How can I copy the 5 attributes from the row at the old location to the row at the new location? Is there a way to move the row, instead of insert/delete?
Thanks.
-
QAbstractItemView::startDrag() is only using the selectedIndexes (QAbstractItemViewPrivate::selectedDraggableIndexes()) so I would suggest to reimplement QAbstractItemModel::mimeData() and return all items, the same then for QAbstractItemModel::setMimeDatta().
-
@Christian-Ehrlicher said in Custom QTableView and model: InternalMove also causes insert/delete:
QAbstractItemView
Christian,
Thanks for taking the time to reply. Let me try and explain my situation again.I have a custom row object with 10 attributes. Only 5 of the attributes are displayed in the tableview. The other 5 attributes are private data which I need for database identity, etc.
I want to drag/drop row only within the table. Basically I want to move the row from one index in the model to another index.
Qt by default does not seem to support row moves. By default, it inserts a new row at the new index, copies the 5 attributes that are visible in the tableview, and then deletes the old row at the old index. The other 5 attributes in the row that are NOT displayed in the tableview, are NOT copied over to the new row.
How do I copy the 5 other attributes from the row at the old location, to the row at the new location before the old row is deleted? Ideally, I would like to move the row. But looks like even InternalMove does an insert/copy/delete. If the copy
-
@JohnGa said in Custom QTableView and model: InternalMove also causes insert/delete:
How do I copy the 5 other attributes from the row at the old location, t
I already explained how to do so - you have to reimplement mimeData() / setMimeData().
-
@Christian-Ehrlicher
Now I get it. The data is transferred from the old row to the new row via the mimeData and setMimeData. By overriding, instead of it picking the visibile table columns/properties I can send all the attributes.Thanks.
-
@Christian-Ehrlicher
Now I get it. The data is transferred from the old row to the new row via the mimeData and setMimeData. By overriding, instead of it picking the visibile table columns/properties I can send all the attributes.Thanks.
I marked the wrong message as the correct answer by mistake, and it won't let me change it. The first reply by @Christian-Ehrlicher is the correct answer. Full credit to @Christian-Ehrlicher
-
Hi,
It has been fixed.