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...
Bookmarks