Question

I am trying to drag a list item onto another list. The dragEnterEvent fires just fine, I do e.accept(), however the dropEvent never fires. Here's the code:

class LocalList(QtGui.QListWidget):
    def __init__(self, parent):
        super(LocalList, self).__init__(parent)
        self.parent = parent
        self.setDragEnabled(True)

    def mouseMoveEvent(self, e):
        mimeData = QtCore.QMimeData()
        mimeData.setText(self.currentItem().text())
        drag = QtGui.QDrag(self)
        drag.setMimeData(mimeData)
        dropAction = drag.exec_()

class RemoteList(QtGui.QListWidget):
    def __init__(self, parent):
        super(RemoteList, self).__init__(parent)
        self.parent = parent
        self.setAcceptDrops(True)

    def dragEnterEvent(self, e):
        print "MimeText: " + e.mimeData().text()
        e.accept()

    def dropEvent(self, e):
        print "DROPPED"
        print self.parent.localdir + "/" + e.mimeData().text()
        e.accept()

To clarify, I'm dragging from LocalList to RemoteList. The mousMoveEvent is being fired just fine, because the mimeData().text() prints out just fine in RemoteList's dragEnterEvent. I don't think it's accepting right though, because dropEvent is never fired, and when I'm hovering over the RemoteList it doesn't have the "drop here" icon.

Était-ce utile?

La solution

You have to implement both dragEnterEvent and dragMoveEvent. See here for another similar question

This seems to do what you need rather short and elegant :)

class DragDropListWidget(QtGui.QListWidget):
    def __init__(self, type, parent=None):
        super(DragDropListWidget, self).__init__(parent)
        self.setDefaultDropAction(QtCore.Qt.MoveAction)
        self.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
        self.setAcceptDrops(True)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top