PDA

View Full Version : QMainWindow not receiving DragEnter events originating from a different application



jonks
9th August 2010, 06:06
Hi,

Is there anything wrong with this code?
It outputs all events the application receives.

I'm trying to get an application to accept drag events that originate from a different application. I noticed that my dragEnterEvent was not being called.

I wrote a minimal application (see below) to test.

The weird thing is I just upgraded from 4.6.1 to 4.6.3 and an application that used to received dragEnterEvent stopped doing so immediately. But I'm not sure right now if that is a co-incidence (I've been changing some code unrelated to DnD).

Here's the minimal project:

Header:


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT
protected:
bool eventFilter(QObject* o,QEvent* e);
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H


CPP file:


#include <QDebug>
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

qApp->installEventFilter(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

bool MainWindow::eventFilter(QObject* o,QEvent* e)
{
qDebug() << e->type() << o; // e->type is never 60 (QEvent::DragEnter)
if(e->type()==QEvent::DragEnter)
qDebug() << "QEvent::DragEnter"; // THIS IS NEVER OUTPUT
return false;
}

saa7_go
9th August 2010, 07:42
I'm trying to get an application to accept drag events that originate from a different application. I noticed that my dragEnterEvent was not being called.

Do you mean accepting drop event?



The weird thing is I just upgraded from 4.6.1 to 4.6.3 and an application that used to received dragEnterEvent stopped doing so immediately. But I'm not sure right now if that is a co-incidence (I've been changing some code unrelated to DnD).

Here's the minimal project:

Header:


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT
protected:
bool eventFilter(QObject* o,QEvent* e);
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H


CPP file:


#include <QDebug>
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

qApp->installEventFilter(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

bool MainWindow::eventFilter(QObject* o,QEvent* e)
{
qDebug() << e->type() << o; // e->type is never 60 (QEvent::DragEnter)
if(e->type()==QEvent::DragEnter)
qDebug() << "QEvent::DragEnter"; // THIS IS NEVER OUTPUT
return false;
}


I think you forgot to set QWidget::setAcceptDrops() to true in your MainWindow constructor.

jonks
9th August 2010, 13:44
Just added this to the minimal project's constructor:


setAcceptDrops(true);


Now the project built with 4.6.1 works.
However, the same project built with 4.6.3 still does not work.

saa7_go
9th August 2010, 14:01
Maybe, you need to read about Drag and Drop (http://doc.trolltech.com/4.6/dnd.html).

jonks
9th August 2010, 15:20
Thanks for the link. I know how to implement it. I've been using DnD for over 12 months now in a big project.

More info.

If I run the exe from Explorer (Windows) after dropping the dll dependencies into the debug dir, then DnD works just fine, for BOTH 4.6.1 and 4.6.3 builds.

If I run the SAME executable from the 4.6.1 Creator gui, then DnD does work.
If I run the SAME executable from the 4.6.3 Creator gui, then DnD does NOT work.

In other words, I think there is something wrong with 4.6.3 Creator.

I do have both Create 4.6.1 and 4.6.3 installed. Perhaps that is a problem?
I'll remove 4.6.1 in a moment to see what happens.

jonks
10th August 2010, 04:39
Uninstalled 4.6.1 (leaving just 4.6.3) installed and hey presto everything works again without any modifications.