Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Drop event not generated after an accepted Drag Enter event

Drop event not generated after an accepted Drag Enter event

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      Violet Giraffe
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved