QTableView Drag and Drop - Drop Prohibited Icon

Heads up! You've already completed this tutorial.

Edward_Lipson | 2022-08-09 11:20:23 UTC | #1

I am trying to use drag and drop to reorder the rows in a QTableView with a single column I searched and found things about setting the QTableView options and the model flags:

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

QT Designer set QTableView:
        self.tableViewList = QTableView(DialogList)
        self.tableViewList.setObjectName(u"tableViewList")
        self.tableViewList.setGeometry(QRect(10, 50, 521, 301))
        self.tableViewList.setDragEnabled(True)
        self.tableViewList.setDragDropOverwriteMode(False)
        self.tableViewList.setDragDropMode(QAbstractItemView.InternalMove)
        self.tableViewList.setDefaultDropAction(Qt.MoveAction)
        self.tableViewList.setSelectionMode(QAbstractItemView.SingleSelection)
        self.tableViewList.setSelectionBehavior(QAbstractItemView.SelectRows)
        self.tableViewList.horizontalHeader().setStretchLastSection(True)

I can see the drag happening but I get the prohibited (slashed red circle) icon for the drop, and the drop does not happen.

Any hints on how to correct this? I saw in searches that the item (model flags) needs to needs to be drop disabled, but that does not make sense to me since I want to drop it. I did try ~Qt.ItemIsDropEnabled but that did not make any difference.

I saw other searches which pointed to moveRow and moveRows. Since I subclassed QAbstractTableView, do I need to implement those methods?


Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

QTableView Drag and Drop - Drop Prohibited Icon was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.