PDA

View Full Version : QDragEnterEvent not being called



Nomad_Tech
9th September 2014, 06:51
#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:

#include "mainwindow.h"


MainWindow::MainWindow(QWidget *parent)
{
setupUi(this);
setAcceptDrops(false);
}


and the DropArea Code:

#include "droparea.h"

DropArea::DropArea(QWidget *parent) :
QLabel(parent)
{
this->setAcceptDrops(true);
}

void DropArea::dragEnterEvent(QDragEnterEvent *event)
{
if(event->mimeData()->hasUrls())
{
event->acceptProposedAction();
}
}

void DropArea::dropEvent(QDropEvent *event)
{
event->acceptProposedAction();
QList<QUrl> urls = event->mimeData()->urls();
if(urls.isEmpty())
{
return;
}
QString fileName = urls.first().toLocalFile();
qDebug("File Name is: " + fileName.toLatin1());
}



Declaration of DropArea:

#ifndef DROPAREA_H
#define DROPAREA_H

#include <QLabel>
#include <QDragEnterEvent>
#include <QMimeData>
#include <QDropEvent>
#include <QEvent>

class DropArea : public QLabel
{
Q_OBJECT
public:
explicit DropArea(QWidget *parent = 0);

protected:
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);

signals:

public slots:

};

#endif // DROPAREA_H

anda_skoa
11th September 2014, 10:18
Is there any instance of your DropArea class in the widget tree created by setupUi?

Cheers,
_

Nomad_Tech
12th September 2014, 01:19
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.