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:
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?
PyQt/PySide 1:1 Coaching with Martin Fitzpatrick — Save yourself time and frustration. Get one on one help with your Python GUI projects. Working together with you I'll identify issues and suggest fixes, from bugs and usability to architecture and maintainability.