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. How to drag file from OSX Finder to QT Gui
QtWS25 Last Chance

How to drag file from OSX Finder to QT Gui

Scheduled Pinned Locked Moved Unsolved General and Desktop
drag&dropgui developmentcreator
3 Posts 3 Posters 1.6k 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.
  • S Offline
    S Offline
    shellback3
    wrote on 14 Dec 2015, 20:32 last edited by
    #1

    This is my first attempt to use creator for a project.

    I've managed to use Qt to recreate the functionality of a small C++ app that allows the user to choose one or more binary data files and decode them to text files suitable for importation into a spreadsheet. It's become apparent that my boss, the only user, wants to use the drag and drop to drag a file or files from Finder to the app's icon or opened app rather than drill down to the files from my app.

    I'd like to implement this so that either the folder or files can be dragged and dropped but this I'm new at Qt and am not sure how to proceed. In creator I've checked the 'Allow drops' property for central widget but don't know how to receive the drop (except they would probably be added to a list).

    I could use a little direction here ...

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 14 Dec 2015, 21:59 last edited by mrjj
      #2

      Hi and welcome
      Disclaimer: only tried on windows. but Qt is cross platform so might just work :)
      MyWindow being a QMainWindow/other widget
      so you add

      protected:
      void dragEnterEvent(QDragEnterEvent *e);
      void dropEvent(QDropEvent *e):
      
      

      to the mainwinow.h file.
      Then implement following:

      MyWindow::MyWindow(QWidget *parent) <<< dont copy this as you will have constructor already
      {
         .... other stuff--
          setAcceptDrops(true); <<< insert this
      }
      
      void MyWindow::dragEnterEvent(QDragEnterEvent *e)
      {
          if (e->mimeData()->hasUrls()) {
              e->acceptProposedAction(); // say yes please
          }
      }
      
      void MyWindow::dropEvent(QDropEvent *e)
      {
          foreach (const QUrl &url, e->mimeData()->urls()) {
              const QString &fileName = url.toLocalFile();
              qDebug() << "Dropped file:" << fileName;
          }
      }
      

      all is in here
      http://doc.qt.io/qt-5.5/dnd.html

      1 Reply Last reply
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 14 Dec 2015, 23:40 last edited by
        #3

        Hi,

        To add to @mrjj, if you want to add support for the dock icon drop, take a look at QOpenFileEvent There's an example there. Follow the link, the example is not part of Qt 5.5's documentation but will be in 5.6

        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
        1

        2/3

        14 Dec 2015, 21:59

        • Login

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