Skip to content

Drag and drop in itemviews occasionally crashes #3

Description

@pathmann

Occasionally (can't reproduce it) the hosting app will crash when starting to drag an item in any itemview.

The reason seems to be a call to mimeTypes in the PythonQtShell implementation of the model.

Script to reproduce this:

from PythonQt.QtCore import Qt, QMimeData
from PythonQt.QtGui import QTreeView, QStandardItemModel, QStandardItem, QAbstractItemView

class mymodel(QStandardItemModel):
    def __init__(self, rows, cols, parent=None):
        super().__init__(rows, cols, parent)

        self.mimecache = []

    def flags(self, index):
        default = Qt.ItemIsSelectable | Qt.ItemIsEnabled

        if index.isValid():
            return default | Qt.ItemIsDragEnabled | Qt.ItemIsDropEnabled
        else:
            return default

    def mimeTypes(self):
        return ["text/plain"]

    def supportedDropActions(self):
        return Qt.MoveAction

    def mimeData(self, indexes):
        try:
            mimedata = QMimeData()
            mimedata.setText(" ".join(map(lambda x: x.data(), indexes)))
            self.mimecache.append(mimedata) #we have to keep this in scope!

            return mimedata
        except Exception as e:
            return 0

    def canDropMimeData(self, mimedata, action, row, column, parent):
        if not mimedata.hasText() or not parent.isValid() or not action & self.supportedDropActions() or row != -1:
            return False

        return True

    def dropMimeData(self, mimedata, action, row, column, parent):
        if action != Qt.MoveAction:
            return False

        if not mimedata.hasText():
            return False

        if mimedata.text() == "":
            return True

        paritem = self.itemFromIndex(parent)
        nitem = QStandardItem(mimedata.text())
        paritem.appendRow(nitem)

        return True


tv = QTreeView(None)
tv.setAttribute(Qt.WA_DeleteOnClose)

model = mymodel(4, 0, tv)
for row in range(4):
    model.setItem(row, 0, QStandardItem("%s" % row))


tv.setDragEnabled(True)
tv.setDropIndicatorShown(True)
tv.setDragDropMode(QAbstractItemView.InternalMove)
tv.setModel(model)
tv.show()

No activity

Activity on this issue will appear here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions