Drag and Drop in QAbstractItemModel PySide PyQt

I studied how to implement QAbstractItemModel with QTreeView and the custom class Item, and everything works for me, except for drag and drop.

Ultimately, I would like to be able to switch between moving and copying elements using the toggle key, but for now, I'm just trying to get InternalMove to work at all.

I am reimplementing mimeData and dropMimeData like this ....

class BuildModel( QAbstractItemModel ):
    def __init__( self, root):
        super( BuildModel, self ).__init__()

    def mimeTypes( self ):
        return ['sushi-build-items']

    def mimeData( self, indices ):
        mimedata = QMimeData()
        mimedata.setData('sushi-build-items', self.getSerializedData(indices) )
        return mimedata

    def dropMimeData( self, mimedata, action, row, column, parentIndex ):
        if not mimedata.hasFormat( 'sushi-build-items' ):
            return False
        data = pickle.loads((str(mimedata.data('sushi-build-items'))))
        items = dataToItems(data)
        self.insertItems(row, items, parentIndex)
        return True

    def insertItems( self, row, items, parentIndex):
        parent = self.itemFromIndex(parentIndex)
        self.beginInsertRows( parentIndex, row, row+len(items)-1 )
        if row == -1:
            parent.addChildren(items)
        else:
            parent.insertChildren(row, items)
        self.endInsertRows()
        self.dataChanged.emit(parentIndex, parentIndex)
        return True

And my tree view is set to InternalMove, like this ....

class TreeView(QTreeView):
    def __init__(self, parent = None, model = None):

        super(TreeView, self).__init__(parent = parent)
        self.setDragDropMode(QAbstractItemView.InternalMove)
        self.setDragEnabled(True)
        self.setAcceptDrops(True)

But when I drag the source element, it remains as it is, and it just removes the repeating element. Shouldn't TreeView handle the handling of a draggable item? If not, where to remove it manually?

I am convinced that something is missing here.

+4
1

removeRows ; .

+3

Source: https://habr.com/ru/post/1534503/