<div dir="ltr"><div>Hi Maurizio and others,</div><div><br></div><div>Just getting back to this thread to share some supplementary information.<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Apr 18, 2021 at 8:35 PM Maurizio Berti <<a href="mailto:maurizio.berti@gmail.com">maurizio.berti@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">An important thing to remember is that you can't always completely rely on IDEs for debugging: not only they will probably hide the actual exception depending on their implementations, but they normally don't show the full traceback. When in doubt, just run your program from terminal/prompt and check the output.</div><div dir="ltr"><div><br></div><div>In your case, the crash is caused by the fact that you didn't correctly implement setData(), which **always** expects a bool as the returned value.</div><div><br></div><div>Then, the problem is that you didn't implement the most important function for drop events in a model: dropMimeData().</div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></blockquote><div><br></div><div>When I read your sentence above, I interpreted it to mean that implementing dropMimeData() was a *required* step to get drag and drop working. Not sure if that was what you really meant, but experimenting more and looking at the source code (a good idea I got from you!), I finally realized that there is a pretty appropriate default dropMimeData() implementation already, which does pretty much the same thing as the implementation you suggested.</div><div><br></div><div>So the program below implements drag and drop without reimplementing dropMimeData at all. Which makes sense given that the "application/x-qabstractitemmodeldatalist" format is not documented, so users shouldn't really have to know how to use it.</div><div><br></div><div>Just sharing in case someone else searches for similar things in the future.</div><div><br></div><div>Cheers,</div><div><br></div><div>Rodrigo</div><div><br></div><div>---------<br></div><div></div>import sys<br><br>from PyQt5 import QtCore, QtWidgets<br>from PyQt5.QtCore import Qt, QDataStream, QIODevice, QModelIndex<br>from PyQt5.QtWidgets import QApplication, QMainWindow, QTableView, QAbstractItemView<br><br># Attempting to implement drag-and-drop for an QAbstractTableModel as described in<br># <a href="https://doc.qt.io/archives/qt-5.5/model-view-programming.html#using-drag-and-drop-with-item-views">https://doc.qt.io/archives/qt-5.5/model-view-programming.html#using-drag-and-drop-with-item-views</a><br><br>class TableModel(QtCore.QAbstractTableModel):<br>    def __init__(self):<br>        super().__init__()<br>        self.dataList = [["Lion", "Tiger", "Bear"], ["Gazelle", "Ox", "Pig"], ["Mouse", "Cat", "Dog"], ["Cow", "Giraffe", "Rhino"]]<br><br>    def data(self, index, role=None):<br>        if role == Qt.DisplayRole:<br>            return f"{self.dataList[index.row()][index.column()]}"<br><br>    def setData(self, index, value, role):<br>        self.dataList[index.row()][index.column()] = value<br>        self.dataChanged.emit(index, index)<br>        return True<br><br>    def rowCount(self, index):<br>        return len(self.dataList)<br><br>    def columnCount(self, index):<br>        return 3<br><br>    def flags(self, index):<br>        return Qt.ItemIsSelectable | Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled | Qt.ItemIsDropEnabled<br><br>    def supportedDropActions(self):<br>        return Qt.CopyAction | Qt.MoveAction<br><br><br>class MainWindow(QMainWindow):<br>    def __init__(self):<br>        super().__init__()<br><br>        self.setWindowTitle("Drag and Drop app")<br><br>        self.tableView = QTableView()<br><br>        self.model = TableModel()<br>        self.tableView.setModel(self.model)<br>        self.tableView.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)<br><br>        self.tableView.setSelectionMode(QAbstractItemView.ExtendedSelection)<br>        self.tableView.setDragEnabled(True)<br>        self.tableView.viewport().setAcceptDrops(True)<br>        self.tableView.setDropIndicatorShown(True)<br>        self.tableView.setDragDropMode(QAbstractItemView.DragDrop)<br>        self.tableView.setDragDropOverwriteMode(False)<br><br>        self.setCentralWidget(self.tableView)<br><br><br>app = QApplication(sys.argv)<br>w = MainWindow()<br>w.show()<br>app.exec_()</div></div>