How can I do drag and drop in a widget?

Drag and drop support in Qt is centered around the QDrag [qt.nokia.com] class that handles most of the details of a drag and drop operation.

In addition to creating a QDrag object, you need to reimplement dragMoveEvent() [qt.nokia.com] to accept the event and dropEvent() [qt.nokia.com] to handle the data dropped on the widget. Finally dragEnterEvent() [doc.qt.nokia.com] needs to be reimplemented to accept the event.

You also need to call

  1. setAcceptDrops(true)

on the widgets that should be able to accept a drop.

For more information, read the general documentation [qt.nokia.com] on drag and drop:

The example below illustrates how this can be done.

  1. #include <QtGui>
  2.  
  3. class MyDialog : public QWidget
  4. {
  5.     Q_OBJECT
  6. public:
  7.     MyDialog(QWidget *parent = 0);
  8. public slots:
  9.     void makeDrag();
  10. protected:
  11.     void dropEvent(QDropEvent *de);
  12.     void dragMoveEvent(QDragMoveEvent *de);
  13.     void dragEnterEvent(QDragEnterEvent *event);
  14. };
  15.  
  16. class MyGroupBox : public QGroupBox
  17. {
  18. public:
  19.     MyGroupBox(QWidget *parent = 0) : QGroupBox("GroupBox", parent) {};
  20. protected:
  21.     void dropEvent(QDropEvent *de);
  22.     void dragMoveEvent(QDragMoveEvent *de);
  23.     void dragEnterEvent(QDragEnterEvent *event);
  24. };
  25.  
  26.  
  27. MyDialog::MyDialog(QWidget *parent) : QWidget(parent)
  28. {
  29.     QHBoxLayout *layout = new QHBoxLayout(this);
  30.     QPushButton *pushButton = new QPushButton("Click Me", this);
  31.     layout->addWidget(pushButton);
  32.     connect(pushButton, SIGNAL(pressed()), this, SLOT(makeDrag()));
  33.     MyGroupBox *box = new MyGroupBox(this);
  34.     layout->addWidget(box);
  35.     // Allow media to be dropped on the widget
  36.     setAcceptDrops(true);
  37.     box->setAcceptDrops(true);
  38. }
  39.  
  40. void MyDialog::makeDrag()
  41. {
  42.     QDrag *dr = new QDrag(this);
  43.     // The data to be transferred by the drag and drop operation is contained in a QMimeData object
  44.     QMimeData *data = new QMimeData;
  45.     data->setText("This is a test");
  46.     // Assign ownership of the QMimeData object to the QDrag object.
  47.     dr->setMimeData(data);
  48.     // Start the drag and drop operation
  49.     dr->start();
  50. }
  51.  
  52. void MyDialog::dragMoveEvent(QDragMoveEvent *de)
  53. {
  54.     // The event needs to be accepted here
  55.     de->accept();
  56. }
  57.  
  58. void MyDialog::dragEnterEvent(QDragEnterEvent *event)
  59. {
  60.     // Set the drop action to be the proposed action.
  61.     event->acceptProposedAction();
  62. }
  63.  
  64. void MyDialog::dropEvent(QDropEvent *de)
  65. {
  66.     // Unpack dropped data and handle it the way you want
  67.     qDebug("Contents: %s", de->mimeData()->text().toLatin1().data());
  68. }
  69.  
  70. void MyGroupBox::dropEvent(QDropEvent *de)
  71. {
  72.     // Unpack dropped data and handle it the way you want
  73.     qDebug("Contents: %s", de->mimeData()->text().toLatin1().data());
  74. }
  75.  
  76. void MyGroupBox::dragMoveEvent(QDragMoveEvent *de)
  77. {
  78.     // The event needs to be accepted here
  79.     de->accept();
  80. }
  81.  
  82. void MyGroupBox::dragEnterEvent(QDragEnterEvent *event)
  83. {
  84.     // Set the drop action to be the proposed action.
  85.     event->acceptProposedAction();
  86. }
  87.  
  88. #include "main.moc"
  89.  
  90. int main(int argc, char **argv)
  91. {
  92.     QApplication a(argc, argv);
  93.     MyDialog d;
  94.     d.show();
  95.     return a.exec();
  96. }

No comments

Write a comment

Sorry, you must be logged in to post a comment.