tags:

views:

30

answers:

1

The Qt documentation "Mac Differences" page provides the following code for accessing an application's bundle path:

CFURLRef appUrlRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
CFStringRef macPath = CFURLCopyFileSystemPath(appUrlRef, kCFURLPOSIXPathStyle);
const char *pathPtr = CFStringGetCStringPtr(macPath,CFStringGetSystemEncoding());
qDebug("Path = %s", pathPtr);
CFRelease(appUrlRef);
CFRelease(macPath);

However, what is the advantage of that over something simpler, like the following:

QDir dir = QDir(QCoreApplication::applicationDirPath());
dir.cdUp();
dir.cdUp();
return dir;
+1  A: 

Never use the first code. As written in the Qt documentation there, it might not work in a non-English environment, due to the fact that the file name encoding is not by CFStringGetSystemEncoding(), which returns the primary non-unicode encoding of the user. Instead, the file name is always encoded by UTF8 (with a slight variant.)

const char *pathPtr = CFStringGetCStringPtr(macPath, kCFStringEncodingUTF8);

More precisely, you need to use CFStringGetFileSystemRepresentation.

QCoreApplication::applicationDirPath() (mostly) correctly takes into account these subtleties, so you should use the latter approach, if you want your app to work on non-English Macs.

Yuji
Interesting... I wonder why Nokia puts that in the documentation despite its problems? I believe I've seen the second way written elsewhere in the documentation (or perhaps in one of the examples).
Jake Petroules
I guess that documentation is a very old one written long time ago. The code itself uses the (mostly) correct approach, and is well-maintained. You should file a bug report.
Yuji