[英]Swap QTreeView items on drag and drop operation
Let's say we have the following hierarchy: 假设我们有以下层次结构:
+ A
|-- B
|-- D
|-- C
And let's say the user drags C onto B. Is it possible to have both nodes (including children) swapped as the result of that drag and drop operation? 并且假设用户将C拖到B.是否可以将这两个节点(包括子节点)作为拖放操作的结果进行交换?
+ A
|-- C
|-- B
|-- D
I've tried multiple approaches: from subclassing the item model ( dropMimeData
, insertRows
, etc) to adjusting modes with setDragDropOverwriteMode
and DragDropMode::InternalMove
, to subclassing dropEvent(QDropEvent * e)
and nothing worked so far. 我尝试了多种方法:从子类化项目模型( dropMimeData
, insertRows
等)到使用setDragDropOverwriteMode
和DragDropMode::InternalMove
调整模式, setDragDropOverwriteMode
dropEvent(QDropEvent * e)
并且到目前为止没有任何工作。
I've encountered the following techincal limitations: 我遇到了以下技术限制:
QStandardItemModel::dropMimeData
and QTreeview::dropEvent
won't let you know the source index, they forward mime data, and there's no way to obtain the source's QModelIndex to perform the swapping. QStandardItemModel::dropMimeData
和QTreeview::dropEvent
不会让你知道源索引,它们转发mime数据,并且无法获得源的QModelIndex来执行交换。
The DragDropMode::InternalMove
and setDragDropOverwriteMode
approach doesn't work as expected, the source node gets appended to the drop target node somehow, and the source node is removed. DragDropMode::InternalMove
和setDragDropOverwriteMode
方法无法按预期工作,源节点以某种方式附加到放置目标节点,并删除源节点。 If you set DragDropMode::Copy
, the source node is preserved, but the target node isn't overwritten either. 如果设置DragDropMode::Copy
,则会保留源节点,但也不会覆盖目标节点。
A nudge in the right direction would be appreciated. 向正确的方向轻推将是值得赞赏的。
I've inspect source code of Qt and it turns out that to fetch source index you should use current selection. 我检查了Qt的源代码,结果发现要获取源索引你应该使用当前选择。 So override QTreeview::dropEvent
and fetch current selection by calling selectedIndexes() . 因此,通过调用selectedIndexes()来覆盖QTreeview::dropEvent
并获取当前选择。
If you have own custom model you have to do it directly on your data and just emit proper signals about moving items. 如果您有自己的自定义模型,则必须直接对数据进行操作,并发出有关移动项目的正确信号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.