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 implement Drag&Drop files in Qt 5.4 QML application in OS X
QtWS25 Last Chance

How to implement Drag&Drop files in Qt 5.4 QML application in OS X

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 1.8k 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.
  • R Offline
    R Offline
    ribtoks
    wrote on last edited by
    #1

    Global purpose is just to Drag&Drop a file to QML App and get real file path.

    Qt DropArea has drop.urls property which is list of NSUrl from OS X. Qt has QUrl::toNSUrl method (which as i understood i have to use in objective-c), but I don't know how to integrate it with my other (C++) code.

    Can somebody give me an example?

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

      Hi,

      Mixing C++ and Objective-C is pretty simple, have a look at the QtMacExtras sources for example

      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
      • R Offline
        R Offline
        ribtoks
        wrote on last edited by
        #3

        Yes, I saw them, thank you, but I've no idea how to translate NSUrl to local filepath using Qt Mac tricks. Also, wasn't able to google it.
        If you can, please provide any minor example..

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

          I meant it for you to see how to mix the two together.
          To get a QUrl from a NSUrl you can use
          @
          QUrl myUrl = QUrl::fromNSURL(yourNSURL);
          @

          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
          • R Offline
            R Offline
            ribtoks
            wrote on last edited by
            #5

            Hm.. Thank you. I'll try it. I'll post back when I'll have some results..

            1 Reply Last reply
            0
            • R Offline
              R Offline
              ribtoks
              wrote on last edited by
              #6

              Ok, solved now. SGaist, Thank you very much!

              Code which works:
              osxnsurlhelper.h
              @
              #ifndef OSXNSURLHELPER
              #define OSXNSURLHELPER

              #include <QUrl>

              namespace Helpers {
              QUrl fromNSUrl(const QUrl &url);
              }

              #endif // OSXNSURLHELPER
              @

              osxnsurlhelper.mm
              @
              #include "osxnsurlhelper.h"
              #include <Foundation/Foundation.h>

              namespace Helpers {
              QUrl fromNSUrl(const QUrl &url) {
              NSURL *nsUrl = url.toNSURL();
              NSString *path = nsUrl.path;

                  QString qtString = QString::fromNSString(path);
                  return QUrl::fromLocalFile(qtString&#41;;
              }
              

              }
              @

              .pro
              @
              OBJECTIVE_SOURCES +=
              Helpers/osxnsurlhelper.mm

              LIBS += -framework Foundation
              @

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

                Why don't you just use

                @url.toLocalFile()@

                ?

                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
                • R Offline
                  R Offline
                  ribtoks
                  wrote on last edited by
                  #8

                  https://bugreports.qt.io/browse/QTBUG-40449
                  In OS X it doesn't work now.. I use it in Windows and Linux without problems..

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

                    Right, forgot about that one...

                    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