Thomas Ayoub
Thomas Ayoub

Reputation: 29431

Qt drop a file | mac os

I've done many searches (leading me to this and that) and adding a few lines to my classes

MainWindow.cpp

#include <QtGui/QDragEnterEvent>
#include <QtGui/QDragLeaveEvent>
#include <QtGui/QDragMoveEvent>
#include <QtGui/QDropEvent>
#include <QtCore/QMimeData>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    ....
    setAcceptDrops(true);
}

void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
    event->acceptProposedAction();
}

void MainWindow::dropEvent(QDropEvent *event)
{
    qDebug() << "On Drop Event";
    const QMimeData* mimeData = event->mimeData();

    if (mimeData->hasUrls())
    {
        QStringList pathList;
        QList<QUrl> urlList = mimeData->urls();

        for (int i = 0; i < urlList.size() && i < 32; ++i)
        {
            pathList.append(urlList.at(i).toLocalFile());
        }

        if(openFiles(pathList))
            event->acceptProposedAction();
    }
}
void MainWindow::dragMoveEvent(QDragMoveEvent * event)
{
event->acceptProposedAction();
}
void MainWindow::dragLeaveEvent(QDragLeaveEvent* event)
{
    event->accept();
}

But I can't drop a file onto my MainWindow (from the finder). It's not that my code crashes or does not compile, it's just that I literally can't. No reaction from the MainWindow, no highlight, nothing.

What am I missing?

Upvotes: 1

Views: 1132

Answers (1)

TheDarkKnight
TheDarkKnight

Reputation: 27611

I suspect you should also be overloading the dragMoveEvent: -

void QWidget::dragMoveEvent(QDragMoveEvent * event)

As the docs state: -

This event handler is called if a drag is in progress, and when any of the following conditions occur: the cursor enters this widget, the cursor moves within this widget, or a modifier key is pressed on the keyboard while this widget has the focus. The event is passed in the event parameter.

There's an example of a Qt drag and drop here. Specifically this is a good reference.

Upvotes: 1

Related Questions