Results 1 to 4 of 4

Thread: Qt5 drag 'n drop problem : dropEvent never called

  1. #1
    Join Date
    Jun 2015
    Posts
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Qt5 drag 'n drop problem : dropEvent never called

    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;

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt5 drag 'n drop problem : dropEvent never called

    Are drag enter and move event handlers being called?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2015
    Posts
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt5 drag 'n drop problem : dropEvent never called

    Yes I checked with GDB that drag "enter" and "move" event handlers are called. I still don't have a clue...

  4. #4
    Join Date
    Jun 2015
    Posts
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt5 drag 'n drop problem : dropEvent never called

    I found the solution : basically what I was trying to do was to have the widget a parent (ie. QFrame) while calling the constructor. I didn't consider the fact that you could add the BeaconWidgetDrag object directly to a Layout with addWidget.

Similar Threads

  1. Replies: 4
    Last Post: 28th April 2014, 11:23
  2. Replies: 2
    Last Post: 25th June 2012, 20:14
  3. Replies: 4
    Last Post: 24th August 2011, 23:33
  4. dropEvent() is never called
    By kervich in forum Qt Programming
    Replies: 1
    Last Post: 10th September 2010, 14:13
  5. Drag and Drop, dropEvent not being called?
    By steg90 in forum Qt Programming
    Replies: 36
    Last Post: 22nd May 2007, 08:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.