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
QtWS25 Last Chance

Drag n' drop does not work

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 821 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.
  • QT-static-prgmQ Offline
    QT-static-prgmQ Offline
    QT-static-prgm
    wrote on 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
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #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
      • QT-static-prgmQ Offline
        QT-static-prgmQ Offline
        QT-static-prgm
        wrote on last edited by
        #3

        works, thanks :D

        1 Reply Last reply
        1

        • Login

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