May 11, 2011

PSI-lbc PSI-lbc
Lab Rat
39 posts

QCoreApplication::applicationDirPath()..gives different results on Win7 and Mac??

 

Trying to access a file in a subdirectory from where the executable is located.

The code reads as:

  1. QString duh = QCoreApplication::applicationFilePath() + "/" + "Help" + "/" + "myHelp.html";

Running Qt Creator on Windows “duh” is equal to:

  1. /Users/PSI/Projects/QT SFA 4.7.2/SFA-build-desktop/SFA.app/Help/myHelp.html

Running Qt Creator on Mac “duh” is equal to:

  1. /Users/PSI/Projects/QT SFA 4.7.2/SFA-build-desktop/SFA.app/Contents/MacOS/SFA/Help/myHelp.html

For whatever reason, the Mac version inserts “/Contents/MacOS/SFA/” after output for the call to QCoreApplication::applicationFilePath()” where the call on the Windows code does not.

Is this a Mac idiosyncrasy or am I missing a Mac setting that will give the actual directory that the executable is in??

I’ve tried running the code through Creator and the “/Contents/MacOS/SFA/” is added resulting that the file I’m trying to load is not found. Same Result “double clicking” on the Mac executable outside of Creator.

On Windows the call to “QCoreApplication::applicationFilePath() work as “expected”, resulting in the file being loaded.

Is there another method to get the “root directory” of the application that works regardless of platform and regardless of whether it a dev or runtime environment?

4 replies

May 12, 2011

Tobias Hunger Tobias Hunger
Mad Scientist
3224 posts

I guess that is where the mac puts the actual executable. Mac puts everything belonging to a program into one directory and then makes the user perceive that as the application in its UI.

May 12, 2011

Volker Volker
Robot Herder
5428 posts

application file path it the path to the actual executable.

On the Mac, applications are packed as so called application bundles. Technially, these are just directories in the file system, with a suffix “.app”. The Finder treats those specially decorated directories as a single file. There are more types of bundles on the Mac. The layout of those are defined by apple in Bundle Programming Guide [developer.apple.com]

The base line is: the actual executable path is MyFancApplication.app/Contents/MacOS/MyFancyApplication

May 13, 2011

PSI-lbc PSI-lbc
Lab Rat
39 posts

>> that is where the mac puts the actual executable
>>The Finder treats those specially decorated directories as a single file.

Thanks guys.

Aaahhhh, lightblub goes on. Looking at the “app package” it appeared like it was the “.exe”.

When I double clicked on the “app package”, the app ran. If I right click on it, and select “show package contents”, the package “directory” is displayed in a “finder like” dialog window that you can drag files or directories into.

Problem solved. Just a Mac learning curve.

May 13, 2011

Volker Volker
Robot Herder
5428 posts

Just for some nitpicking: the new window actually is a Finder window :-) The same works for (double) clicking on a folder while holding the cmd-key.

Ah, BTW: The nice thing is of course, the Qt creates the app bundle for you :)

 
  ‹‹ QAbstractItemModel: How to signal major change?      Static linking Qt libraries ››

You must log in to post a reply. Not a member yet? Register here!