<div dir="ltr"><div>Hi,</div><div><br></div><div>I want to use drag and drop for a QAbstractItemModel. I followed the instructions at <a href="https://doc.qt.io/archives/qt-5.5/model-view-programming.html#using-drag-and-drop-with-item-views">Qt's documentation</a> quite carefully, but when I try a drag-and-drop, the program crashes.<br></div><div><br></div><div>I'm using PyQt 5.9.2 / Qt 5.9.7 installed with conda, on Windows 10.

</div><div><br></div><div>Any idea what I am doing wrong here? Thanks.</div><div><br></div><div><pre style="background-color:rgb(255,255,255);color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9.8pt"><span style="color:rgb(0,51,179)">import </span>sys<br><br><span style="color:rgb(0,51,179)">from </span>PyQt5 <span style="color:rgb(0,51,179)">import </span>QtCore, QtWidgets<br><span style="color:rgb(0,51,179)">from </span>PyQt5.QtCore <span style="color:rgb(0,51,179)">import </span>Qt<br><span style="color:rgb(0,51,179)">from </span>PyQt5.QtWidgets <span style="color:rgb(0,51,179)">import </span>QApplication, QMainWindow, QTableView, QAbstractItemView<br><br><span style="color:rgb(140,140,140);font-style:italic"># Attempting to implement drag-and-drop for an QAbstractTableModel as described in<br></span><span style="color:rgb(140,140,140);font-style:italic"># <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></span><span style="color:rgb(140,140,140);font-style:italic"><br></span><span style="color:rgb(0,51,179)">class </span><span style="color:rgb(0,0,0)">TableModel</span>(QtCore.QAbstractTableModel):<br>    <span style="color:rgb(0,51,179)">def </span><span style="color:rgb(178,0,178)">__init__</span>(<span style="color:rgb(148,85,141)">self</span>):<br>        <span style="color:rgb(0,0,128)">super</span>().<span style="color:rgb(178,0,178)">__init__</span>()<br>        <span style="color:rgb(148,85,141)">self</span>.dataList = [[<span style="color:rgb(0,128,128);font-weight:bold">"Lion"</span>, <span style="color:rgb(0,128,128);font-weight:bold">"Tiger"</span>, <span style="color:rgb(0,128,128);font-weight:bold">"Bear"</span>], [<span style="color:rgb(0,128,128);font-weight:bold">"Gazelle"</span>, <span style="color:rgb(0,128,128);font-weight:bold">"Ox"</span>, <span style="color:rgb(0,128,128);font-weight:bold">"Pig"</span>], [<span style="color:rgb(0,128,128);font-weight:bold">"Mouse"</span>, <span style="color:rgb(0,128,128);font-weight:bold">"Cat"</span>, <span style="color:rgb(0,128,128);font-weight:bold">"Dog"</span>]]<br><br>    <span style="color:rgb(0,51,179)">def </span><span style="color:rgb(0,0,0)">data</span>(<span style="color:rgb(148,85,141)">self</span>, index, role=<span style="color:rgb(0,51,179)">None</span>):<br>        <span style="color:rgb(0,51,179)">if </span>role == Qt.DisplayRole:<br>            <span style="color:rgb(0,51,179)">return </span><span style="color:rgb(0,128,128);font-weight:bold">f"</span><span style="color:rgb(0,55,166)">{</span><span style="color:rgb(148,85,141)">self</span>.dataList[index.row()][index.column()]<span style="color:rgb(0,55,166)">}</span><span style="color:rgb(0,128,128);font-weight:bold">"<br></span><span style="color:rgb(0,128,128);font-weight:bold"><br></span><span style="color:rgb(0,128,128);font-weight:bold">    </span><span style="color:rgb(0,51,179)">def </span><span style="color:rgb(0,0,0)">setData</span>(<span style="color:rgb(148,85,141)">self</span>, index, value, role):<br>        <span style="color:rgb(0,51,179)">if </span>role == Qt.EditRole:<br>            <span style="color:rgb(148,85,141)">self</span>.dataList[index.row()][index.column()] = value<br>            <span style="color:rgb(0,51,179)">return True<br></span><span style="color:rgb(0,51,179)"><br></span><span style="color:rgb(0,51,179)">    def </span><span style="color:rgb(0,0,0)">rowCount</span>(<span style="color:rgb(148,85,141)">self</span>, index):<br>        <span style="color:rgb(0,51,179)">return </span><span style="color:rgb(0,0,128)">len</span>(<span style="color:rgb(148,85,141)">self</span>.dataList)<br><br>    <span style="color:rgb(0,51,179)">def </span><span style="color:rgb(0,0,0)">columnCount</span>(<span style="color:rgb(148,85,141)">self</span>, index):<br>        <span style="color:rgb(0,51,179)">return </span><span style="color:rgb(23,80,235)">3<br></span><span style="color:rgb(23,80,235)"><br></span><span style="color:rgb(23,80,235)">    </span><span style="color:rgb(0,51,179)">def </span><span style="color:rgb(0,0,0)">flags</span>(<span style="color:rgb(148,85,141)">self</span>, index):<br>        <span style="color:rgb(0,51,179)">return </span>Qt.ItemIsSelectable | Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled | Qt.ItemIsDropEnabled<br><br>    <span style="color:rgb(0,51,179)">def </span><span style="color:rgb(0,0,0)">supportedDropActions</span>(<span style="color:rgb(148,85,141)">self</span>):<br>        <span style="color:rgb(0,51,179)">return </span>Qt.CopyAction | Qt.MoveAction<br><br>    <span style="color:rgb(0,51,179)">def </span><span style="color:rgb(0,0,0)">insertRows</span>(<span style="color:rgb(148,85,141)">self</span>, row: <span style="color:rgb(0,0,128)">int</span>, count: <span style="color:rgb(0,0,128)">int</span>, parent) -> <span style="color:rgb(0,0,128)">bool</span>:<br>        <span style="color:rgb(148,85,141)">self</span>.beginInsertRows(parent, row, row + count)<br>        <span style="color:rgb(148,85,141)">self</span>.dataList[row : row + count] = [<span style="color:rgb(0,128,128);font-weight:bold">""</span>, <span style="color:rgb(0,128,128);font-weight:bold">""</span>, <span style="color:rgb(0,128,128);font-weight:bold">""</span>] * count<br>        <span style="color:rgb(148,85,141)">self</span>.endInsertRows()<br>        <span style="color:rgb(0,51,179)">return True<br></span><span style="color:rgb(0,51,179)"><br></span><span style="color:rgb(0,51,179)">    def </span><span style="color:rgb(0,0,0)">removeRows</span>(<span style="color:rgb(148,85,141)">self</span>, row: <span style="color:rgb(0,0,128)">int</span>, count: <span style="color:rgb(0,0,128)">int</span>, parent) -> <span style="color:rgb(0,0,128)">bool</span>:<br>        <span style="color:rgb(148,85,141)">self</span>.beginRemoveRows(parent, row, row + count)<br>        <span style="color:rgb(0,51,179)">del </span><span style="color:rgb(148,85,141)">self</span>.dataList[row : row + count]<br>        <span style="color:rgb(148,85,141)">self</span>.endRemoveRows()<br>        <span style="color:rgb(0,51,179)">return True<br></span><span style="color:rgb(0,51,179)"><br></span><span style="color:rgb(0,51,179)"><br></span><span style="color:rgb(0,51,179)">class </span><span style="color:rgb(0,0,0)">MainWindow</span>(QMainWindow):<br>    <span style="color:rgb(0,51,179)">def </span><span style="color:rgb(178,0,178)">__init__</span>(<span style="color:rgb(148,85,141)">self</span>):<br>        <span style="color:rgb(0,0,128)">super</span>().<span style="color:rgb(178,0,178)">__init__</span>()<br><br>        <span style="color:rgb(148,85,141)">self</span>.setWindowTitle(<span style="color:rgb(0,128,128);font-weight:bold">"A drag-and-drop table app"</span>)<br><br>        <span style="color:rgb(148,85,141)">self</span>.tableView = QTableView()<br><br>        <span style="color:rgb(148,85,141)">self</span>.model = TableModel()<br>        <span style="color:rgb(148,85,141)">self</span>.tableView.setModel(<span style="color:rgb(148,85,141)">self</span>.model)<br>        <span style="color:rgb(148,85,141)">self</span>.tableView.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)<br><br>        <span style="color:rgb(148,85,141)">self</span>.tableView.setSelectionMode(QAbstractItemView.ExtendedSelection)<br>        <span style="color:rgb(148,85,141)">self</span>.tableView.setDragEnabled(<span style="color:rgb(0,51,179)">True</span>)<br>        <span style="color:rgb(148,85,141)">self</span>.tableView.viewport().setAcceptDrops(<span style="color:rgb(0,51,179)">True</span>)<br>        <span style="color:rgb(148,85,141)">self</span>.tableView.setDropIndicatorShown(<span style="color:rgb(0,51,179)">True</span>)<br>        <span style="color:rgb(148,85,141)">self</span>.tableView.setDragDropMode(QAbstractItemView.InternalMove)<br><br>        <span style="color:rgb(148,85,141)">self</span>.setCentralWidget(<span style="color:rgb(148,85,141)">self</span>.tableView)<br><br><br>app = QApplication(sys.argv)<br>w = MainWindow()<br>w.show()<br>app.exec_()<br></pre></div><br></div>