How to reference Resources folder in code
Question
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?
Solution
QApplication::applicationDirPath()
http://doc.qt.io/qt-5/qcoreapplication.html#applicationDirPath
OTHER TIPS
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" );
When you compile your product, have your tried setting the path of Resources
to be relative? Otherwise, you can retrieve the main bundle, the URL of the app thereof and append it to the Resources URL.
There's a manual for everything, it seems :)