Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 933
Kong Chee Hong Avatar asked Oct 12 '11 09:10

Kong Chee Hong


1 Answers

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.

like image 192
alexisdm Avatar answered Oct 20 '22 11:10

alexisdm