Results 1 to 2 of 2

Thread: Where's the app bundle looking for my media folder? Ok on Qt Creator not in Terminal.

  1. #1
    Join Date
    Jun 2010
    Posts
    36
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Where's the app bundle looking for my media folder? Ok on Qt Creator not in Terminal.

    I'm having a little trouble making my (mac os x) "app bundle" work from the terminal or just double clicking it.

    This App actually compiles, links and runs perfectly within the Qt Creator IDE. But, if I try to open it from the terminal I get a "media/file.x file not found" error. The App bundle nor the /Contents/MacOS/executable is finding the "media" folder that is supposed to be beside the executable.

    In my app I do something like:
    Qt Code:
    1. openFile("media/file.x");
    To copy to clipboard, switch view to plain text mode 

    In Windows and Linux, this file WILL be found if the "media" folder is exactly in the same hierarchical position of the executable (beside it). On the Mac I have discovered it works differently cause Qt Creator builds an "App Bundle" and the actual executable is inside the /Contents/MacOS folder, so I copied the "media" manually there. This worked without any hassle when "playing" my app from the Qt Creator but as mentioned before it doesn't work when using the bundle itself.

    So does anyone know where or how can I homogenize the look for this "media" folder so it works on both Qt Creator and the App bundle itself?

    Lately, I have been using the following command to "install" the folder on the bundle.
    Qt Code:
    1. mac {
    2. MediaFiles.files = media
    3. MediaFiles.path = Contents/MacOS
    4. QMAKE_BUNDLE_DATA += MediaFiles
    5. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for your help.
    Last edited by toglia3d; 4th October 2011 at 02:22.

  2. #2
    Join Date
    Jun 2010
    Posts
    36
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Where's the app bundle looking for my media folder? Ok on Qt Creator not in Termi

    After looking for a few days I found a couple of posts discussing the relative path problem. I was just not searching with the right words... The answer is displayed in:

    http://stackoverflow.com/questions/5...ing-in-xcode-c

    http://www.experimentgarden.com/2009...from-your.html

    Basically it is necessary to add this:

    Qt Code:
    1. //On the include part
    2. #ifdef __APPLE__
    3. #include "CoreFoundation/CoreFoundation.h"
    4. #endif
    5.  
    6.  
    7. // On the main of your app
    8. // This makes relative paths work in C++ in Xcode by changing directory to the Resources folder inside the .app bundle
    9. #ifdef __APPLE__
    10. CFBundleRef mainBundle = CFBundleGetMainBundle();
    11. CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
    12. char path[PATH_MAX];
    13. if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX))
    14. {
    15. // error!
    16. }
    17. CFRelease(resourcesURL);
    18.  
    19. chdir(path);
    20. std::cout << "Current Path: " << path << std::endl;
    21. #endif
    22. // ----------------------------------------------------------------------------
    To copy to clipboard, switch view to plain text mode 

    **IMPORTANT:
    The chdir, changes the working path of the app, so it is not necessary to change you relative code after that...**

Similar Threads

  1. Replies: 1
    Last Post: 11th April 2012, 09:50
  2. Replies: 0
    Last Post: 13th October 2010, 05:27
  3. Replies: 5
    Last Post: 21st May 2010, 02:48
  4. Default folder in Qt Creator
    By brunelch in forum Qt Tools
    Replies: 1
    Last Post: 16th July 2009, 18:07

Tags for this Thread

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.