我使用 QTreeView 来显示从 QAbstractItemModel 派生的自定义模型。
我想通过拖放启用在 QTreeView 内移动树项,因此在 QTreeView 上使用了 setDragDropMode(QAbstractItemView.InternalMove)
。这适用于在视图内移动项目,但现在也允许将项目拖到其他元素上,例如所有窗口标题栏或 Firefox 中的书签栏。在那里删除项目会导致树项目从视图中删除。
这是 QAbstractItemView.InternalMove
的预期行为吗?文档将此选项指定为 The view accepts move (not copy) operations only from itself.
(source),这意味着我不能将其拖到视图之外。
我在这里遗漏了一个不同的属性吗?甚至可以只允许在视图内拖放吗?文档对此非常含糊。
编辑:使用 PyQt 的最小的、可重现的示例
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
app = QApplication(sys.argv)
model = QStandardItemModel()
model.setRowCount(10)
model.setColumnCount(1)
for i in range(0, 10):
model.setData(model.index(i, 0), 'Row %d' % i, Qt.DisplayRole)
view = QTreeView()
view.setDragDropMode(QAbstractItemView.InternalMove)
view.setModel(model)
view.show()
app.exec_()