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. Drag n' drop does not work
Forum Update on Monday, May 27th 2025

Drag n' drop does not work

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 827 Views
  • 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.
  • Q Offline
    Q Offline
    QT-static-prgm
    wrote on 2 Jan 2017, 15:33 last edited by
    #1

    Hi,

    i want to enable drag n' drop for my program, but it does not work. I want to accept files (only .msh but it would be fine if it accept everything, too)to drop on my window and i need the filename and path.

    First i set my widget to accept drops
    and then i overwrite the drop function
    But when i move something over my window, i get an icon that dropping is not allowed.

    Any ideas?? Do i need the move function, too?? I don't care about where the file is dropped. It can be done everywhere on my window.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 2 Jan 2017, 15:49 last edited by mrjj 1 Feb 2017, 15:58
      #2

      Hi
      Yes, you need the other funcs also
      http://doc.qt.io/qt-5/dnd.html
      So it knows its ok and will show "ok" symbol.

      void Window::dragEnterEvent(QDragEnterEvent *event)
      {
      if (event->mimeData()->hasFormat("text/plain"))
      event->acceptProposedAction();
      }

      Also notice the text/plain check.
      When dropping files from outside , the format will/might be different.
      something like

      void MainWindow::dragEnterEvent(QDragEnterEvent *e)
      {
          if (e->mimeData()->hasUrls()) {
              e->acceptProposedAction();
          }
      }
      
      void MainWindow::dropEvent(QDropEvent *e)
      {
          foreach (const QUrl &url, e->mimeData()->urls()) {
              QString fileName = url.toLocalFile();
              qDebug() << "Dropped file:" << fileName;
          }
      }
      

      source:
      http://stackoverflow.com/questions/14895302/qt-drag-drop-add-support-for-dragging-files-to-the-applications-main-window

      • It can be done everywhere on my window.
        No, it only works for the widget that implements the D&D.
        So if u drag over another widget it wont work.
      1 Reply Last reply
      2
      • Q Offline
        Q Offline
        QT-static-prgm
        wrote on 3 Jan 2017, 10:40 last edited by
        #3

        works, thanks :D

        1 Reply Last reply
        1

        1/3

        2 Jan 2017, 15:33

        • Login

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