Qt - access to the package path

The Qt " Mac Differences " documentation contains the following code to access the application package 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 this simpler one, for example:

QDir dir = QDir(QCoreApplication::applicationDirPath());
dir.cdUp();
dir.cdUp();
return dir;
+3
source share
2 answers

Modern way with Qt 5 and OS X 10.9 or higher:

CFURLRef url = (CFURLRef)CFAutorelease((CFURLRef)CFBundleCopyBundleURL(CFBundleGetMainBundle()));
QString path = QUrl::fromCFURL(url).path();
0
source

Never use the first code . As written in the Qt documentation, it may not work in a non-English environment due to the fact that the file name encoding is not CFStringGetSystemEncoding(), which returns the main non-Unicode encoding of the user. Instead, the file name is always encoded in UTF8 (with a small option).

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

, CFStringGetFileSystemRepresentation.

QCoreApplication::applicationDirPath() ( ) , , , Mac.

+3

Source: https://habr.com/ru/post/1759811/


All Articles