[PyQt] Drag and Drop from one list into another with Model/View

Nathan Hüsken nathan.huesken at posteo.de
Mon Oct 3 14:33:33 BST 2011


Hi,

I have an application with 2 listviews (and corresponding models). The
first list view lists items. The second lists references to this items
(which can occure multiple times).

Now I want support drag and drop such that items from the first list
viewcan be dropped in the second.

So I try enable drag and drop by doing the following:
In the main window:

    self.ui.firstListView.setDragEnabled(True)
    self.ui.secondListView.setAcceptDrops(True)

now for the model of the first ListView:

    class FirstModel(QtCore.QAbstractListModel):
      ...
      def flags(self, index):
        return QtCore.QAbstractListModel.flags(self, index) |
QtCore.Qt.ItemIsDragEnabled
      def mimeTypes(self):
        return ["application/vnd.text.list"]

And for the second:

    class SecondModel(QtCore.QAbstractListModel):
      ...
      def mimeTypes(self):
        return ["application/vnd.text.list"]
      def dropMimeData(self, data, action, row, column, parent = None):
        print "dropMimeData"

Running the application I can drag items from the first listview, but I
can not drop it into the second (dropMimeData is never called).
What am I missing here?

Also:
As soon as I start dragging items, I get:
TypeError: invalid result type from FirstModel.rowCount()

rowCount returns an Int. Why do I get this message?

Thanks!


More information about the PyQt mailing list