I'm porting a c++ Qt application from Windows to OSX and cannot wrap my head around the .app bundle concept. I hope someone can help me understand.
My executable lives here: MyProgram.app/Content/MacOS/MyProgram.exe
My resource folder lives here: MyProgram.app/Content/Resources/
In my code I use a relative path to reference items in the resource folder:
"../Resources/something.png"
This works great if I open the .app bundle and run the .exe directly.
But that is not how the .app bundle is meant to work. The user is supposed to click on the .app bundle in the Finder to run the program. But in that case my relative path no longer works, and this is what I don't understand.
Does anyone understand my problem and how I can fix it?
-
When you compile your product, have your tried setting the path of
Resourcesto be relative? Otherwise, you can retrieve the main bundle, the URL of the app thereof and append it to the Resources URL. -
Bundles and the Resource Manager
There's a manual for everything, it seems :)
-
http://stackoverflow.com/questions/516200/relative-paths-not-working-in-xcode-c
This solution helped me immensely. Thanks for the help everyone!
-
QApplication::applicationDirPath()
http://doc.trolltech.com/4.5/qcoreapplication.html#applicationDirPath
JimDaniel : I like this solution the best as it doesn't depend on platform-specific code. Thanks!Nick : Or QCoreApplication::applicationDirPath() - works on non-gui based Qt apps. -
We use:
QDir CoreDir::bundle() { // Trolltech provided example CFURLRef appUrlRef = CFBundleCopyBundleURL( CFBundleGetMainBundle() ); CFStringRef macPath = CFURLCopyFileSystemPath( appUrlRef, kCFURLPOSIXPathStyle ); QString path = CFStringToQString( macPath ); CFRelease(appUrlRef); CFRelease(macPath); return QDir( path ); }So do CoreDir::bundle().filePath( "../Resources" );
0 comments:
Post a Comment