Congratulations to our 2022 Qt Champions!

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) :
    bool FileDropHandler::eventFilter(QObject* /*watched*/, QEvent* event)
    	if (event->type() == QEvent::DragEnter)
    		auto * e = static_cast<QDragEnterEvent*>(event);
    		if (e->mimeData()->hasUrls())
    	else if (event->type() == QEvent::Drop)
    		auto * e = static_cast<QDropEvent*>(event);
    		FileNamesContainer filePaths;
    		for (const QUrl& url: e->mimeData()->urls())
    	return false;

    FileDropHandler is, of course, a subclass of QObject. And I'm instantiating it for a certain QTextEdit. Problem: DragEnter event is processed and acceptProposedAction is executed, but the Drop event does not follow. What did I miss?

  • return true after e->acceptProposedAction() fixed it. And I get it. What I don't get is this: I have tried e->accept() before posting this question, and it did not help. Can someone please explain?

  • Lifetime Qt Champion


    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

Log in to reply