[英]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: 您最好的选择是:
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
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不能从移动/线程”的错误。 Do mind that you'll have to visually tune the QTimer
repeated events accordingly. 请注意,您必须相应地视觉调整
QTimer
重复事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.