I need to call some Objective C API from my C++ code. So I created two files - Foo.mm and Foo.h, and included them in the project pro file as
macx {
HEADERS += Foo.h
OBJECTIVE_SOURCES += Foo.mm
}
macx {
HEADERS += Foo.h
OBJECTIVE_SOURCES += Foo.mm
}
To copy to clipboard, switch view to plain text mode
I am dragging and dropping a file on my main window, and I need the actual path of that file, but there is a bug in Qt-Yosemite combination, due to which some bizarre path is being printed, so I need to call native APIs to make it work, as discussed here. So here is my code in Foo.mm:
{
qDebug() << "First: " << url.toString().toStdString().c_str() << endl; //prints correctly
qDebug() << "Orig: " << aPath.toStdString().c_str() << endl; //prints correctly
if (url.
host().
isEmpty() && url.
path().
startsWith(QLatin1String("/.file/id="))) {
url
= QUrl::fromNSURL([url.
toNSURL() filePathURL
]);
qDebug() << "Inside: " << url.toString().toStdString().c_str() << endl; //this one prints it blank
}
url.
setPath(url.
path().
normalized(QString::NormalizationForm_C));
qDebug() << "Here: " << url.toString().toStdString().c_str() << endl; //thus it is blank as well
return url.toString();
}
QString returnActualPathForDroppedFile(QString aPath)
{
QUrl url(aPath);
qDebug() << "First: " << url.toString().toStdString().c_str() << endl; //prints correctly
qDebug() << "Orig: " << aPath.toStdString().c_str() << endl; //prints correctly
if (url.host().toLower() == QLatin1String("localhost"))
url.setHost(QString());
if (url.host().isEmpty() && url.path().startsWith(QLatin1String("/.file/id=")))
{
url = QUrl::fromNSURL([url.toNSURL() filePathURL]);
qDebug() << "Inside: " << url.toString().toStdString().c_str() << endl; //this one prints it blank
}
url.setPath(url.path().normalized(QString::NormalizationForm_C));
qDebug() << "Here: " << url.toString().toStdString().c_str() << endl; //thus it is blank as well
return url.toString();
}
To copy to clipboard, switch view to plain text mode
After I call the method from my C++ code with the file path of the file being dropped, it comes here, but after the call to the filePathURL method, it returns a blank, whereas as per the patch, it should return the correct path (I once compiled Qt from source using the patch, it works fine, so the patch is correct).
The error message I get is this: CFURLCreateFilePathURL failed because it was passed this URL which has no scheme: /.file/id=6571367.17566290
How do I fix this?
Platform - Qt 5.3.1 32 bit, OS X Yosemite.
Bookmarks