Hi all,

I'm facing a problem with Drag 'N Drop : I started my design using Fridge Magnets exemple. But now I'm trying to integrate the modified code to my main app. Thing is I can't drop my widget anymore. I suspect it has to do with the fact that I'm now trying to drag 'n drop my widgets on a QFrame, whereas on the Qt Exemple it doesn't go like that.

I've pasted some code :
Qt Code:
  1. BeaconWidgetDrag::BeaconWidgetDrag(QWidget *parent)
  2. : QWidget(parent)
  3. {
  4. BeaconWidget * test;
  5.  
  6. int x = 5;
  7. int y = 5;
  8.  
  9. test = new BeaconWidget("x01", BeaconStateDown, this);
  10.  
  11. test->move(x, y);
  12. test->show();
  13. test->setAttribute(Qt::WA_DeleteOnClose);
  14.  
  15. //setMinimumSize(400, qMax(200, y));
  16. setAcceptDrops(true);
  17. }
  18.  
  19. BeaconWidgetDrag::~BeaconWidgetDrag()
  20. {
  21.  
  22. }
  23.  
  24. void BeaconWidgetDrag::dragEnterEvent(QDragEnterEvent *event)
  25. {
  26. if (event->mimeData()->hasFormat("application/x-beaconwidget")) {
  27. if (children().contains(event->source())) {
  28. event->setDropAction(Qt::MoveAction);
  29. event->accept();
  30. } else {
  31. event->acceptProposedAction();
  32. }
  33. } else if (event->mimeData()->hasText()) {
  34. event->acceptProposedAction();
  35. } else {
  36. event->ignore();
  37. }
  38. }
  39.  
  40. void BeaconWidgetDrag::dragMoveEvent(QDragMoveEvent *event)
  41. {
  42. if (event->mimeData()->hasFormat("application/x-beaconwidget")) {
  43. if (children().contains(event->source())) {
  44. event->setDropAction(Qt::MoveAction);
  45. event->accept();
  46. } else {
  47. event->acceptProposedAction();
  48. }
  49. } else if (event->mimeData()->hasText()) {
  50. event->acceptProposedAction();
  51. } else {
  52. event->ignore();
  53. }
  54. }
  55.  
  56. void BeaconWidgetDrag::dropEvent(QDropEvent *event)
  57. {
  58. if (event->mimeData()->hasFormat("application/x-beaconwidget")) {
  59. const QMimeData *mime = event->mimeData();
  60. QByteArray itemData = mime->data("application/x-beaconwidget");
  61. QDataStream dataStream(&itemData, QIODevice::ReadOnly);
  62.  
  63. QString text;
  64. QPoint offset;
  65. dataStream >> text >> offset;
  66. BeaconWidget * newBeacon = new BeaconWidget(text, BeaconStateDown, this);
  67. newBeacon->move(event->pos() - offset);
  68. newBeacon->show();
  69. newBeacon->setAttribute(Qt::WA_DeleteOnClose);
  70.  
  71. if (event->source() == this) {
  72. event->setDropAction(Qt::MoveAction);
  73. event->accept();
  74. } else {
  75. event->acceptProposedAction();
  76. }
  77. } else if (event->mimeData()->hasText()) {
  78. QStringList pieces = event->mimeData()->text().split(QRegExp("\\s+"),
  79. QString::SkipEmptyParts);
  80. QPoint position = event->pos();
  81.  
  82. foreach (QString piece, pieces) {
  83. BeaconWidget * newBeacon = new BeaconWidget(piece, BeaconStateDown, this);
  84. newBeacon->move(position);
  85. newBeacon->show();
  86. newBeacon->setAttribute(Qt::WA_DeleteOnClose);
  87.  
  88. position += QPoint(newBeacon->width(), 0);
  89. }
  90.  
  91. event->acceptProposedAction();
  92. } else {
  93. event->ignore();
  94. }
  95. }
  96.  
  97. void BeaconWidgetDrag::mousePressEvent(QMouseEvent * event)
  98. {
  99. BeaconWidget *child = static_cast<BeaconWidget *>(childAt(event->pos()));
  100. if (!child)
  101. return;
  102.  
  103. QPoint hotSpot = event->pos() - child->pos();
  104.  
  105. QByteArray itemData;
  106. QDataStream dataStream(&itemData, QIODevice::WriteOnly);
  107. dataStream << child->labelText() << QPoint(hotSpot);
  108.  
  109. QMimeData *mimeData = new QMimeData;
  110. mimeData->setData("application/x-beaconwidget", itemData);
  111. mimeData->setText(child->labelText());
  112.  
  113. QDrag *drag = new QDrag(this);
  114. drag->setMimeData(mimeData);
  115. drag->setPixmap(*child->pixmap());
  116. drag->setHotSpot(hotSpot);
  117.  
  118. child->hide();
  119.  
  120. if (drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction) == Qt::MoveAction)
  121. child->close();
  122. else
  123. child->show();
  124. }
  125.  
  126.  
  127. void BeaconWidgetDrag::Log(QString s)
  128. {
  129. emit LogChanged(s);
  130. }
To copy to clipboard, switch view to plain text mode 


My drag 'n drop widget is called that way, with ui->frame being a QFrame with setAcceptDrops = true
Qt Code:
  1. void BeaconMonitor::on_tabWidget_tabBarDoubleClicked(int index)
  2. {
  3. BeaconWidgetDrag * beaconWidgetDrag = new BeaconWidgetDrag(ui->frame);
  4. beaconWidgetDrag->show();
  5. }
To copy to clipboard, switch view to plain text mode 


I hope I have been clear enough.

Thanks for your help;