简体   繁体   English

在拖放操作上交换QTreeView项

[英]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. 我尝试了多种方法:从子类化项目模型( dropMimeDatainsertRows等)到使用setDragDropOverwriteModeDragDropMode::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::dropMimeDataQTreeview::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::InternalMovesetDragDropOverwriteMode方法无法按预期工作,源节点以某种方式附加到放置目标节点,并删除源节点。 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并获取当前选择。


Swap is not a problem. 交换不是问题。 You have to just do move twice. 你必须做两次。
There is spatial method for that: QAbstractItemModel::moveRow (in threes you have only one column so moving row is ok). 有空间方法: QAbstractItemModel :: moveRow三个你只有一列,所以移动行是好的)。 Here you have a answer with another solutions (for standard item model). 在这里,您可以使用其他解决方案 (标准项目模型)。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM