Trying to implement drag and drop for QAbstractItemModel

Rodrigo de Salvo Braz rodrigobraz at gmail.com
Mon Apr 19 01:15:00 BST 2021


Hi,

I want to use drag and drop for a QAbstractItemModel. I followed the
instructions at Qt's documentation
<https://doc.qt.io/archives/qt-5.5/model-view-programming.html#using-drag-and-drop-with-item-views>
quite carefully, but when I try a drag-and-drop, the program crashes.

I'm using PyQt 5.9.2 / Qt 5.9.7 installed with conda, on Windows 10.

Any idea what I am doing wrong here? Thanks.

import sys

from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableView,
QAbstractItemView

# Attempting to implement drag-and-drop for an QAbstractTableModel as
described in
# https://doc.qt.io/archives/qt-5.5/model-view-programming.html#using-drag-and-drop-with-item-views

class TableModel(QtCore.QAbstractTableModel):
    def __init__(self):
        super().__init__()
        self.dataList = [["Lion", "Tiger", "Bear"], ["Gazelle", "Ox",
"Pig"], ["Mouse", "Cat", "Dog"]]

    def data(self, index, role=None):
        if role == Qt.DisplayRole:
            return f"{self.dataList[index.row()][index.column()]}"

    def setData(self, index, value, role):
        if role == Qt.EditRole:
            self.dataList[index.row()][index.column()] = value
            return True

    def rowCount(self, index):
        return len(self.dataList)

    def columnCount(self, index):
        return 3

    def flags(self, index):
        return Qt.ItemIsSelectable | Qt.ItemIsEditable |
Qt.ItemIsEnabled | Qt.ItemIsDragEnabled | Qt.ItemIsDropEnabled

    def supportedDropActions(self):
        return Qt.CopyAction | Qt.MoveAction

    def insertRows(self, row: int, count: int, parent) -> bool:
        self.beginInsertRows(parent, row, row + count)
        self.dataList[row : row + count] = ["", "", ""] * count
        self.endInsertRows()
        return True

    def removeRows(self, row: int, count: int, parent) -> bool:
        self.beginRemoveRows(parent, row, row + count)
        del self.dataList[row : row + count]
        self.endRemoveRows()
        return True


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("A drag-and-drop table app")

        self.tableView = QTableView()

        self.model = TableModel()
        self.tableView.setModel(self.model)
        self.tableView.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)

        self.tableView.setSelectionMode(QAbstractItemView.ExtendedSelection)
        self.tableView.setDragEnabled(True)
        self.tableView.viewport().setAcceptDrops(True)
        self.tableView.setDropIndicatorShown(True)
        self.tableView.setDragDropMode(QAbstractItemView.InternalMove)

        self.setCentralWidget(self.tableView)


app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210418/b404f93a/attachment.htm>


More information about the PyQt mailing list