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 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

    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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.