Hi Guys,

I have encountered problem in dragging and dropping multiple QLabel and QWidget at the same time (multiple selection of QLabel & QWidget) which I have used in my application. In my application, there are images and texts needed to be drag & drop together.

Can someone please help in solving this problem? Written below are the codes I have written in my MainWindow.cpp file.
Thank You




Qt Code:
  1. void MainWindow::mousePressEvent(QMouseEvent *event)
  2. {
  3. QWidget *child = static_cast<QWidget *>(childAt(event->pos()));
  4. if (!child)
  5. return;
  6.  
  7. QPoint hotSpot = event->pos() - child->pos();
  8. QString textdata = child->styleSheet(); [COLOR="#FF0000"] //not sure if this syntax is correct[/COLOR]
  9.  
  10. QByteArray itemData;
  11. QDataStream dataStream(&itemData, QIODevice::WriteOnly);
  12. dataStream << textdata << hotSpot;
  13.  
  14. QMimeData *mimeData = new QMimeData;
  15. mimeData->setData("mydatatype", itemData);
  16.  
  17. QDrag *drag = new QDrag(this);
  18. drag->setMimeData(mimeData);
  19. drag->setPixmap(textdata);
  20. drag->setHotSpot(hotSpot);
  21.  
  22.  
  23. if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction)
  24. {
  25. child->close();
  26. }
  27. else
  28. {
  29. child->show();
  30. child->setStyleSheet(textdata); [COLOR="#FF0000"]//not sure if this syntax is correct[/COLOR]
  31. }
  32. }
To copy to clipboard, switch view to plain text mode