Kong Chee Hong
Kong Chee Hong

Reputation: 81

Qt Drag and Drop - Change image display while dragging

I started a drag with image.

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

I want to change the image display when my drag reach certain point in the widget.

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

I do this in dragMoveEvent. I'm able to start a new drag with a new image. But I seem to be unable to cancel the first drag. I find that the the previous drag/drop action is still being performed.

Anyone could suggest:

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 do start drag.

Upvotes: 8

Views: 6456

Answers (2)

andrea.marangoni
andrea.marangoni

Reputation: 1499

From QDrag docs:

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

Sets pixmap as the pixmap used to represent the data in a drag and drop operation. You can only set a pixmap before the drag is started.

So, I'm sorry, but you can't! Even setDragCursor() is a method with no effects...

Upvotes: 0

alexisdm
alexisdm

Reputation: 29886

According to Qt's documentation, you can't change the pixmap once the dragging has started.

Qt uses:

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

You could try to use the X11 method on all platforms, or use SetCursor if you only plan to deploy your application on Windows.

Upvotes: 6

Related Questions