Hi all,
My app needs to read configuration files relative to the application directory. I'm on mac and QDir::current() was returning the directory inside the bundle. I found the code below in the docs here and I wrote a function to use with the addition of falling back to QDir::current if it's not mac.
It works great on the mac I don't have access to windows or linux at the moment to test
Since I'm very new to Qt please let me know if the there is any mistake there or if you would approach it differently.
Also if anyone could test it on windows and/or linux to see what it returns I would greatly appreciate it.
#ifdef Q_OS_MAC
#include "CoreFoundation/CoreFoundation.h"
#endif
#ifdef Q_OS_MAC
CFURLRef appUrlRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
CFStringRef macPath = CFURLCopyFileSystemPath(appUrlRef,
kCFURLPOSIXPathStyle);
const char *pathPtr = CFStringGetCStringPtr(macPath,
CFStringGetSystemEncoding());
CFRelease(appUrlRef);
CFRelease(macPath);
QString dirPath
= filePath.
left(filePath.
lastIndexOf("/"));
return dirPath;
#else
return appdir.path();
#endif
}
#ifdef Q_OS_MAC
#include "CoreFoundation/CoreFoundation.h"
#endif
QString AppDir(){
#ifdef Q_OS_MAC
CFURLRef appUrlRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
CFStringRef macPath = CFURLCopyFileSystemPath(appUrlRef,
kCFURLPOSIXPathStyle);
const char *pathPtr = CFStringGetCStringPtr(macPath,
CFStringGetSystemEncoding());
CFRelease(appUrlRef);
CFRelease(macPath);
QString filePath = QString(pathPtr);
QString dirPath = filePath.left(filePath.lastIndexOf("/"));
return dirPath;
#else
QDir appdir = QDir::current();
return appdir.path();
#endif
}
To copy to clipboard, switch view to plain text mode
Thank you
Laszlo
Bookmarks