Qt Drag and Drop - change the image display when dragging and dropping

I started dragging and dropping with the image.

QDrag* drag = new(QDrag)(this); drag->setPixmap(*pixmap); drag->exec(Qt::CopyAction | Qt::MoveAction); 

I want to change the image display when my movement reaches a certain point in the widgets.

I currently have an idea. After I start the first drag, when my resistance reaches a certain point, I cancel the first drag and then restart the new drag with a new image.

I am doing this in dragMoveEvent . I can start a new drag and drop with a new image. But I can't seem to undo the first drag and drop. I believe the previous drag and drop action is still in progress.

Anyone can offer:

 if (event->mimeData()->hasText()) { if (need_to_change_pixmap()) { event->setDropAction(Qt::IgnoreAction); change_pixmap_restart_drag(); } else { event->setDropAction(Qt::MoveAction); event->accept(); } } else { event->ignore(); } 

The change_pixmap_restart_drag function just starts to drag.

+4
source share
2 answers

According to the Qt documentation , you cannot change the pixmap after you start dragging and dropping.

Qt uses:

  • frameless windows that follow the mouse on the X11 . The pixel map is painted into the widget, and setGrabMouse used to receive mouse events, because the widget is not directly under the mouse.
  • SetCursor function on Windows , with a copy of pixmap, where the cursors for each action were drawn in the corner.
  • the SetDragImageWithCGImage function on a Mac , but it needs a DragRef object declared as an inaccessible local variable in qdnd_mac.mm.

You can try using the X11 method on all platforms or use SetCursor if you plan to deploy the application on Windows.

+5
source

From the QDrag docs:

 void QDrag::setPixmap ( const QPixmap & pixmap ) : 

Sets pixmap as pixmap used to represent data in a drag and drop operation. You can set pixmap just before the start of the drag.

So sorry, but you cannot! Even setDragCursor() is a no-effect method ...

0
source

All Articles