PDA

View Full Version : QTableView move internal is very confusing



UglyBob
9th November 2016, 12:50
I'm using PyQT4 and have an application with the GUI designed in QT Designer. In my application I have a custom dialog with a simple QTableView with a custom model extended from QAbstractTableModel. It works fine, but now I'm trying to implement internal move of rows, i.e. drag and drop a single row to a new position. I have tried every combination of drag and drop options in QAbstractItemView (in QT Designer), currently I'm using these settings:

dragEnabled: True
dragDropOverwrite: False
dragDropMode: InternalMove
defaultDropAction: MoveAction
selectionMode: SingleSelection
selectionBehaviour: SelectRows

Then in my model I have implemented the following functions:

rowCount()
columnCount()
headerData()
data()
setData()
flags()
supportedDragActions()
supportedDropActions()
addRow()
insertRows()
removeRow()
removeRows()

The flags I return are the following:
Qt.ItemIsDragEnabled | Qt.ItemIsDropEnabled | Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable

And supportedDragActions/supportedDropActions return:
Qt.CopyAction | Qt.MoveAction

When I test I can see that if I drag a row and drop it exactly between two rows, the row I dragged gets deleted (removeRows() is called) and insertRows() is also called as it should. But if I drop at another row, only removeRows() is called. I guess this is because in the first case the table handle the drop, in the second case, the item handles the drop. But I'm not sure how to solve it...

Added after 39 minutes:

One detail I can not figure out is if I really need to implement dropMimeData() and mimeData() or if it is only if I want to allow external drag and drop...

UglyBob
10th November 2016, 07:18
No one has a working code example of a QTableView/model that supports internal move?

Killian
10th November 2016, 16:00
One detail I can not figure out is if I really need to implement dropMimeData() and mimeData() or if it is only if I want to allow external drag and drop...
You don't need to implement those for purely internal drag'n'drop operations.

Unfortunately I don't know any solution to that other issue. I literally did the same as you but with a QListView. Dropping any item in between two others works but it gets "swallowed" when being dropped onto another item. Couldn't find out how to avoid this or what would be the right way to accomplish this.

So I would also be highly interested in any kind of solution.