Results 1 to 6 of 6

Thread: Please critique my application directory function.

  1. #1
    Join Date
    Jan 2013
    Posts
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Please critique my application directory function.

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Please critique my application directory function.

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2013
    Posts
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Please critique my application directory function.

    Hi,

    It returns the directory inside the bundle Eg: UserDir/MyApp.app/Contents/MacOS which is not what I'm after.
    I need the UserDir so users can place configuration files in there next to the app and if it exists the app loads them otherwise it loads the defaults from resources.

    Laszlo

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Please critique my application directory function.

    Well... QDir::current() will simply not work. It returns the current working directory which has nothing to do with the application directory.

    Placing configuration in the directory containing the bundle is probably a bad idea. What if the bundle is placed somewhere the user has no access rights to? Configuration should be placed somewhere under user's home directory. Otherwise there are too many things that could fail.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2013
    Posts
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Please critique my application directory function.

    Hi,

    You're correct. If the application is deployed to the default Applications folder or other default app directory it wont work.
    However I am writing a host for my gamelib RaptorGL.com. So the app wont be in an application folder rather in the game project directory. When a suer supplied settings.json can't be loaded it loads a default one from Resources, which btw is rockin my world

    So with that background do you see anything in there that could cause issues in other platforms?

    Thank you for your help

    Laszlo

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Please critique my application directory function.

    I told you, QDir::current() won't work.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Application directory path on linux (shortcut)
    By MadBear in forum Qt Programming
    Replies: 2
    Last Post: 11th March 2018, 20:18
  2. How to find Application Data directory?
    By Teuniz in forum Qt Programming
    Replies: 7
    Last Post: 26th July 2013, 14:02
  3. Replies: 0
    Last Post: 15th April 2011, 11:03
  4. Replies: 5
    Last Post: 15th August 2010, 21:34
  5. application directory path on mac
    By munna in forum Qt Programming
    Replies: 0
    Last Post: 27th January 2007, 11:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.