try this:
Qt Code:
  1. void drag::mousePressEvent(QMouseEvent *event)
  2. {
  3. if (event->button() == Qt::LeftButton)
  4. {
  5. QMimeData *mimeData = new QMimeData;
  6. mimeData->setText("myDrageLabel");
  7. QDrag *drag = new QDrag(this);
  8. drag->setMimeData(mimeData);
  9. Qt::DropAction dropAction = drag->start(Qt::CopyAction | Qt::MoveAction);
  10. if (dropAction == Qt::MoveAction)
  11. {
  12. close();
  13. update();
  14. }
  15. }
  16. }
  17. in your base class, in which you are using drag object (say myWidget)
  18. myWidget::myWidget(QWidget *parent) :
  19. QWidget(parent)
  20. {
  21. setAcceptDrops(true);
  22. ...
  23. }
  24. void myWidget::dragEnterEvent(QDragEnterEvent *event)
  25. {
  26. if (event->mimeData()->hasText())
  27. {
  28. event->acceptProposedAction();
  29. }
  30. else
  31. {
  32. event->ignore();
  33. }
  34. }
  35. void myWidget::dropEvent(QDropEvent *event)
  36. {
  37. if (event->mimeData()->hasText())
  38. {
  39. //QString plainText = event->mimeData()->text();
  40. QPoint dropPos = event->pos();
  41. QDragObject *drobj;
  42. if ( pixmap() )
  43. drobj = new QImageDrag( pixmap()->convertToImage(), this );
  44. drobj->move(dropPos);
  45.  
  46. drobj->show();
  47.  
  48. if (children().contains(event->source()))
  49. {
  50. event->setDropAction(Qt::MoveAction);
  51. event->accept();
  52. }
  53. else
  54. {
  55. event->acceptProposedAction();
  56. }
  57. }
  58. else
  59. {
  60. event->ignore();
  61. }
  62. }
To copy to clipboard, switch view to plain text mode