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.

Qt Code:
  1. #ifdef Q_OS_MAC
  2. #include "CoreFoundation/CoreFoundation.h"
  3. #endif
  4.  
  5. QString AppDir(){
  6. #ifdef Q_OS_MAC
  7. CFURLRef appUrlRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
  8. CFStringRef macPath = CFURLCopyFileSystemPath(appUrlRef,
  9. kCFURLPOSIXPathStyle);
  10. const char *pathPtr = CFStringGetCStringPtr(macPath,
  11. CFStringGetSystemEncoding());
  12. CFRelease(appUrlRef);
  13. CFRelease(macPath);
  14. QString filePath = QString(pathPtr);
  15. QString dirPath = filePath.left(filePath.lastIndexOf("/"));
  16. return dirPath;
  17. #else
  18. QDir appdir = QDir::current();
  19. return appdir.path();
  20. #endif
  21. }
To copy to clipboard, switch view to plain text mode 

Thank you

Laszlo