Solved Drop event not generated after an accepted Drag Enter event
-
I'm trying to create a tiny helper class that would facilitate processing drop events for any widget. Here's the definition, it should be pretty clear what the idea is:
FileDropHandler::FileDropHandler(QWidget* widget, const std::function<void (const FileNamesContainer&)>& handler) : QObject(widget), _handler(handler) { assert(widget); assert(handler); widget->setAcceptDrops(true); widget->installEventFilter(this); } bool FileDropHandler::eventFilter(QObject* /*watched*/, QEvent* event) { if (event->type() == QEvent::DragEnter) { auto * e = static_cast<QDragEnterEvent*>(event); if (e->mimeData()->hasUrls()) e->acceptProposedAction(); } else if (event->type() == QEvent::Drop) { auto * e = static_cast<QDropEvent*>(event); FileNamesContainer filePaths; for (const QUrl& url: e->mimeData()->urls()) filePaths.emplace_back(url.toLocalFile()); _handler(filePaths); } return false; }
FileDropHandler
is, of course, a subclass ofQObject
. And I'm instantiating it for a certainQTextEdit
. Problem:DragEnter
event is processed andacceptProposedAction
is executed, but theDrop
event does not follow. What did I miss? -
return true
aftere->acceptProposedAction()
fixed it. And I get it. What I don't get is this: I have triede->accept()
before posting this question, and it did not help. Can someone please explain? -
Hi,
AFAIK,
e-accept()
just tells that the system want the event. Returning true from eventFilter means that the event has been handled and handling should stop there.Hope it helps