我正在使用带有QTableView
的QAbstractItemModel
的子类化版本,并使用子类化的model.dropMimeData()
、model.insertRows()
、model.removeRows()
激活了拖放。现在,我想要显示拖放操作完成后的更改,并让用户再次撤消该操作。因此,我为tableView
实现了自己的dropEvent()
-method。我还将move方法设置为InternalMove
。
我检查方法内部的移动确认,然后调用super(widget.__class__, widget)
.dropEvent(event)
。我期望在此执行之后,行被插入到新位置,并在旧位置被删除。发生的情况是,它在指定位置插入行,但只有在dropEvent()
完成后才删除旧位置的行。无论我是在函数内部调用event.accept()
还是event.acceptProposedAction()
,它都会一直等到dropEvent()
完成。
我正在寻找一个信号,它告诉我拖放操作何时被执行。我希望QAbstractItemModel
的rowsMoved
-signal是我想要的,但它不是在dnd操作期间发出的。然而,信号rowsInserted
和rowsRemoved
是发出的。但rowsRemoved
信号是在dropEvent()
完成时立即发出的。谁知道QTableView
在哪里执行插入目标行、设置数据和删除源行的操作?
我在Windows10上使用python3和PyQt5。
def dropEvent_withConfirm(widget, event):
dropInd = widget.indexAt(event.pos())
if not dropInd.isValid():
event.ignore()
return
confirmGUI = ConfirmGUI("Drag Element to new position?",
"Are you sure you want to drag the element to this position?",
True)
if confirmGUI.getConfirmation():
super(widget.__class__, widget).dropEvent(event)
event.accept()
# Here I want to do something after the finished move, but here the target row was inserted, but the source row is not yet deleted
else:
event.ignore()
self.client.tblView.dropEvent = lambda e: dropEvent_withConfirm(self.client.tblView, e)
发布于 2020-06-25 09:56:59
我现在不依赖于super().dropEvent()
,而是通过自己实现它来解决它。我找不到一个合适的信号,它是在完成drop时发出的。下面是我的更新代码:
def dropEvent_withConfirm(widget, event):
dropInd = widget.indexAt(event.pos())
if not dropInd.isValid():
event.ignore()
return
confirmGUI = ConfirmGUI("Drag Element to new position?",
"Are you sure you want to drag the element to this position?",
True)
if confirmGUI.getConfirmation():
old_row, new_row = getSrcAndDstRow()
entry = getDraggedEntry()
self.myModel.insertRows(new_row)
self.myModel.setRow(new_row, entry)
if old_row > new_row:
self.myModel.removeRows(old_row + 1)
else:
self.myModel.removeRows(old_row)
event.accept()
# Here I can now do something after the finished move
else:
event.ignore()
self.client.tblView.dropEvent = lambda e: dropEvent_withConfirm(self.client.tblView, e)
https://stackoverflow.com/questions/62517678
复制