PDA

View Full Version : Qt embedded Objective C++ code not working properly



Cupidvogel
16th April 2015, 19:34
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
}


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 (http://stackoverflow.com/questions/27093697/os-x-yosemite-qt-drag-and-drop-file-name-bug). So here is my code in Foo.mm:



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::Normali zationForm_C));

qDebug() << "Here: " << url.toString().toStdString().c_str() << endl; //thus it is blank as well
return url.toString();
}


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.

wysota
16th April 2015, 22:31
The URL obviously lacks the scheme. Have you tried file:///?

Cupidvogel
16th April 2015, 22:34
I actually solved the problem right now. Here is the solution:



QString returnActualPathForDroppedFile(QString aPath)
{
QUrl url(aPath);
if (url.host().toLower() == QLatin1String("localhost"))
url.setHost(QString());
if (url.host().isEmpty() && url.path().startsWith(QLatin1String("/.file/id=")))
{
const QByteArray utf8 = aPath.toUtf8();
const char* cString = utf8.constData();
NSString *localFilePath = [[NSString alloc] initWithUTF8String:cString];
NSURL *localFilePathURL = [NSURL fileURLWithPath:localFilePath];
url = QUrl::fromNSURL([localFilePathURL filePathURL]);
}
url.setPath(url.path().normalized(QString::Normali zationForm_C));
return url.toString();
}