简体   繁体   English

Qt拖放QTreeView

[英]Qt Drag and Drop QTreeView

My TreeView has various folder with children. 我的TreeView有各种带有孩子的文件夹。 each child has a icon and text-name. 每个孩子都有一个图标和文本名称。 the user can select one or more item and drag them into QMDI area. 用户可以选择一个或多个项目并将其拖动到QMDI区域。 as image for my darg drop i have a local picture drag->setPixmap(QPixmap(myPixImage)). 作为我的Darg Drop的图像,我有一个本地图片drag-> setPixmap(QPixmap(myPixImage))。 What I need is: how i make the selected item with icon and text as myPixImage dynamically. 我需要的是:如何动态制作带有图标和文本作为myPixImage的所选项目。

It's easy to set the pixmap when your own code create the QDrag object, however using QXxxViews (not only QTreeView) you have only control on the QMimeData (when overriding QAbstractItemModel::mimeData()). 当您自己的代码创建QDrag对象时,设置像素图很容易,但是使用QXxxViews(不仅是QTreeView),您只能控制QMimeData(覆盖QAbstractItemModel :: mimeData()时)。

Therefore if you really need that, the only way I now is to subclass QTreeView, overriding (well... reimplenting) mouseMoveEvent() and the like. 因此,如果您真的需要它,那么我现在唯一的方法是继承QTreeView的子类,重写(以及重新实现)mouseMoveEvent()等。 If you do so, you can get the selected items when creating the QDrag through eg selectionModel()->selectedItems(). 如果这样做,则可以在创建QDrag时通过例如selectionModel()-> selectedItems()获得选定的项目。

First of all: there's no simple way to do this. 首先:没有简单的方法可以做到这一点。 Basically because QDrag::exec is started when you (as developer) allow the start of the drag (example here ) and it doesn't return until the drag is finished. 基本上是因为QDrag::exec是在您(作为开发人员)允许开始拖动时启动的( 此处示例),并且直到拖动完成后它才返回。

You want to change your QDrag object while dragging. 您要拖动时更改QDrag对象。

Your best option here is: 您最好的选择是:

  1. After creating the Drag object and before running QDrag::exec store the object in some pool that can be accessed anywhere in the code. 创建Drag对象之后并运行QDrag::exec 之前,将该对象存储在某个池中,该池可在代码中的任何位置进行访问。 Probably something like static map<QDrag *> pool 可能类似于static map<QDrag *> pool
  2. Set up a recurrent QTimer event that constantly updates the the QDrag object, which can be obtained from the pool. 设置一个循环QTimer事件,该事件不断更新QDrag对象,该对象可以从池中获取。 Do not try using Qthread s as you'll end up with some "QObject can't be moved from/to thread" error. 不要尝试使用Qthread S作为你会用一些落得“的QObject不能从移动/线程”的错误。
  3. When the drag's ended (as user), clean up the object from the pool 拖动结束后(以用户身份),从池中清理对象

Do mind that you'll have to visually tune the QTimer repeated events accordingly. 请注意,您必须相应地视觉调整QTimer重复事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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