JimDaniel
JimDaniel

Reputation: 12703

How to reference Resources folder in code

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?

Upvotes: 2

Views: 7641

Answers (4)

Reed Hedges
Reed Hedges

Reputation: 1650

QApplication::applicationDirPath()

http://doc.qt.io/qt-5/qcoreapplication.html#applicationDirPath

Upvotes: 5

Matt
Matt

Reputation: 10564

Bundle Programming Guide

There's a manual for everything, it seems :)

Upvotes: 0

mxcl
mxcl

Reputation: 26883

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" );

Upvotes: 3

dirkgently
dirkgently

Reputation: 111130

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.

Upvotes: 0

Related Questions