How to implement Drag&Drop files in Qt 5.4 QML application in OS X
-
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?
-
Hi,
Mixing C++ and Objective-C is pretty simple, have a look at the QtMacExtras sources for example
-
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);
@ -
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); }
}
@.pro
@
OBJECTIVE_SOURCES +=
Helpers/osxnsurlhelper.mmLIBS += -framework Foundation
@ -
Why don't you just use
@url.toLocalFile()@
?
-
https://bugreports.qt.io/browse/QTBUG-40449
In OS X it doesn't work now.. I use it in Windows and Linux without problems.. -
Right, forgot about that one...