Results 1 to 3 of 3

Thread: QDragEnterEvent not being called

  1. #1
    Join Date
    Sep 2014
    Location
    Sydney, Australia
    Posts
    16
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QDragEnterEvent not being called

    #Update: Looks like I have an answer, tried on my home computer this evening and it runs fine. Both work and home PCs are using same version of Qt Creator, both with Qt 4.8 and 5.1. I can only assume my work system has become corrupted in some manner. I have removed the non-essential bits from the code just in case anyone is looking for a working drag and drop file example. - 9/9/14

    Hey all,
    I've been searching forums all morning and can't seem to find an answer to this.

    I was adding drag and drop functionality to a serial terminal (Dragging text files from Windows Explorer and Dropping them into the terminal). However the dropEvent never occurs and looking into it further the dragEnterEvent never seems to be called. I ended up closing that project and just trying to get Drag and Drop working on a simple blank application.

    - I have set my MainWindow AcceptDrops to false, Accept Drops is enabled on "DropArea" which is my subclass of QLabel.
    - DragEnterEvent,DragMoveEvent, DragLeaveEvent and DropEvent are reimplemented in my DropArea class.
    - I have a qDebug statement inside DragEnterEvent. This debug statement is never called.
    - I then installed an event filter on my dropArea. When a text file is dragged from explorer no QDragEnterEvent is seen on the eventFilter. When, clicked on however, I see mouse click events ok.
    - A breakpoint inside dragEnterEvent is never reached.
    - The cursor shown while dragging over my application has been the ForbiddenCursor the whole time.

    I have looked at several DnD examples online and the requirements seem to be: enable AcceptDrops on your widget, reimplement dragEnterEvent & dropEvent and ensure parent is not handling the dropEvents.

    What Obvious mistake am I making here? -_-

    Here's my mainwindow code:
    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3.  
    4. MainWindow::MainWindow(QWidget *parent)
    5. {
    6. setupUi(this);
    7. setAcceptDrops(false);
    8. }
    To copy to clipboard, switch view to plain text mode 

    and the DropArea Code:
    Qt Code:
    1. #include "droparea.h"
    2.  
    3. DropArea::DropArea(QWidget *parent) :
    4. QLabel(parent)
    5. {
    6. this->setAcceptDrops(true);
    7. }
    8.  
    9. void DropArea::dragEnterEvent(QDragEnterEvent *event)
    10. {
    11. if(event->mimeData()->hasUrls())
    12. {
    13. event->acceptProposedAction();
    14. }
    15. }
    16.  
    17. void DropArea::dropEvent(QDropEvent *event)
    18. {
    19. event->acceptProposedAction();
    20. QList<QUrl> urls = event->mimeData()->urls();
    21. if(urls.isEmpty())
    22. {
    23. return;
    24. }
    25. QString fileName = urls.first().toLocalFile();
    26. qDebug("File Name is: " + fileName.toLatin1());
    27. }
    To copy to clipboard, switch view to plain text mode 

    Declaration of DropArea:
    Qt Code:
    1. #ifndef DROPAREA_H
    2. #define DROPAREA_H
    3.  
    4. #include <QLabel>
    5. #include <QDragEnterEvent>
    6. #include <QMimeData>
    7. #include <QDropEvent>
    8. #include <QEvent>
    9.  
    10. class DropArea : public QLabel
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit DropArea(QWidget *parent = 0);
    15.  
    16. protected:
    17. void dragEnterEvent(QDragEnterEvent *event);
    18. void dropEvent(QDropEvent *event);
    19.  
    20. signals:
    21.  
    22. public slots:
    23.  
    24. };
    25.  
    26. #endif // DROPAREA_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by Nomad_Tech; 9th September 2014 at 10:53. Reason: updated contents

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDragEnterEvent not being called

    Is there any instance of your DropArea class in the widget tree created by setupUi?

    Cheers,
    _

  3. #3
    Join Date
    Sep 2014
    Location
    Sydney, Australia
    Posts
    16
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDragEnterEvent not being called

    Thanks for your reply skoa, yes a DropArea appears in the "ui_mainwindow.h" file. Interestingly, I tried running the executable outside of Qt Creator and the Drag&Drop functions correctly. It seems the problem occurs only when running my applications from Qt Creator and only on my work PC.

Similar Threads

  1. Destructor not being called
    By vieraci in forum Qt Programming
    Replies: 12
    Last Post: 25th January 2012, 15:06
  2. I receive QDragEnterEvent, but not QDropEvent
    By Ishmael in forum Qt Programming
    Replies: 5
    Last Post: 27th October 2009, 01:25
  3. Why Qt is called Qt?
    By nifei in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2009, 07:40
  4. QDragEnterEvent and acceptProposedAction on mac
    By ntp in forum Qt Programming
    Replies: 0
    Last Post: 14th July 2009, 21:41
  5. filterAcceptsRow is not called.
    By kaushal_gaurav in forum Qt Programming
    Replies: 1
    Last Post: 10th February 2009, 08:05

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.