Results 1 to 6 of 6

Thread: I receive QDragEnterEvent, but not QDropEvent

  1. #1
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default I receive QDragEnterEvent, but not QDropEvent

    I am trying to drag an icon from the desktop onto my application, which is nothing more than an empty QGraphicsView window. I receive a QDragEnterEvent, but when I release the mouse button, I do not receive a QDropEvent. What am I doing wrong? Here's my simplified code. Thanks for your help!

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QMainWindow>
    3. #include <QGraphicsView>
    4. #include <QDragEnterEvent>
    5. #include <QDropEvent>
    6. #include <QGraphicsSceneDragDropEvent>
    7.  
    8. class MyGraphicsView : public QGraphicsView
    9. {
    10. //Q_OBJECT // Do I need Q_OBJECT?
    11.  
    12. public:
    13. MyGraphicsView(QGraphicsScene *parent=0);
    14.  
    15. protected:
    16. void dragEnterEvent(QDragEnterEvent *event);
    17. void dropEvent(QDropEvent *event);
    18. //void dropEvent(QGraphicsSceneDragDropEvent *event);
    19. };
    20.  
    21. MyGraphicsView::MyGraphicsView(QGraphicsScene *parent)
    22. : QGraphicsView(parent)
    23. {
    24. setAcceptDrops(true);
    25. }
    26.  
    27. void MyGraphicsView::dragEnterEvent(QDragEnterEvent *event)
    28. {
    29. //event->setDropAction(Qt::IgnoreAction);
    30. event->accept();
    31. }
    32.  
    33. //void MyGraphicsView::dropEvent(QGraphicsSceneDragDropEvent *event)
    34. //{
    35. // event->accept();
    36. // qDebug("here");
    37. //}
    38.  
    39. void MyGraphicsView::dropEvent(QDropEvent *event)
    40. {
    41. qDebug("here"); // I NEVER GET TO HERE
    42. event->accept();
    43. }
    44.  
    45. int main(int argc, char *argv[])
    46. {
    47. QApplication app(argc, argv);
    48. QMainWindow window;
    49.  
    50. MyGraphicsView *view = new MyGraphicsView(scene);
    51. view->setAcceptDrops(true);
    52.  
    53. window.setCentralWidget(view);
    54. window.resize(640, 480);
    55. window.setAcceptDrops(true);
    56. window.show();
    57.  
    58. return app.exec();
    59. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: I receive QDragEnterEvent, but not QDropEvent

    Call
    Qt Code:
    1. view->viewport()->setAcceptDrops(true);
    To copy to clipboard, switch view to plain text mode 
    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
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: I receive QDragEnterEvent, but not QDropEvent

    Thanks for the response. Unfortunately, it still doesn't work. Do I need to do something with QGraphicsSceneDropEvent?

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

    Default Re: I receive QDragEnterEvent, but not QDropEvent

    Do you get drag move events? It's highly possible the default implementation rejects drags there and hence no drop events are delivered. From what I see in the code, that's exactly what happens - by default you will only get drop events if the scene accepts a drag move event. If you want something else, you have to reimplement dragMoveEvent for the view and accept the drags there.
    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.


  5. The following user says thank you to wysota for this useful post:

    Ishmael (27th October 2009)

  6. #5
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: I receive QDragEnterEvent, but not QDropEvent

    You were right! It's so non-intuitive, I never would have figured it out on my own. Thanks a million for your help!

    For anyone else who might have this problem, here's what I added to my code to get it to accept Drop events. Why I have to accept Move events to get a Drop event, I shall never know, but here it is! :-)

    Qt Code:
    1. void MyGraphicsView::dragMoveEvent(QDragMoveEvent *event)
    2. {
    3. event->accept();
    4. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // And then to extract the filename(s) for the dragged icon(s)
    2. // Taken from "dropsite" example.
    3. void MyGraphicsView::dropEvent(QDropEvent *event)
    4. {
    5. const QMimeData *mimeData = event->mimeData();
    6. if (mimeData->hasUrls()) {
    7. QList<QUrl> urlList = mimeData->urls();
    8. QString filenames;
    9. for (int i = 0; i < urlList.size() && i < 32; ++i) {
    10. QString url = urlList.at(i).path();
    11. filenames += url + QString("\n");
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: I receive QDragEnterEvent, but not QDropEvent

    Quote Originally Posted by Ishmael View Post
    It's so non-intuitive, I never would have figured it out on my own.
    Actually I'd say it is quite intuitive once you know how dragging works in Qt and what the relation between the graphics view and graphics scene is.

    Why I have to accept Move events to get a Drop event, I shall never know, but here it is! :-)
    Because you might want to accept drops only on certain areas of a widget and the user needs a visual hint to know if he can make a drop at a certain place before he decides to do it.
    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.


Similar Threads

  1. Replies: 6
    Last Post: 10th August 2009, 04:12
  2. UDP datagram receive
    By mdannenb in forum Qt Programming
    Replies: 8
    Last Post: 27th July 2008, 03:30

Tags for this Thread

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.