How to implement Drag&Drop files in Qt 5.4 QML application in OS X
-
wrote on 13 Jan 2015, 19:31 last edited by
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
-
wrote on 13 Jan 2015, 21:03 last edited by
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.. -
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);
@ -
wrote on 14 Jan 2015, 07:25 last edited by
Hm.. Thank you. I'll try it. I'll post back when I'll have some results..
-
wrote on 14 Jan 2015, 08:26 last edited by
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()@
?
-
wrote on 15 Jan 2015, 05:46 last edited by
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...
8/9