Path changing on Mac [solved]

  • Hi there. I am having a strange problem. I build an App in Qt5 that I want to run also in a Mac. The App needs to write a small TXT file. On windows, there is no problem, since it writes on the folder where the executable is. But on the Mac, the current path changes depending on how you run the App:

    • If I double-click the .app file, the current path is / (root) and I get permission denied.

    • If I run from Qt-Creator, the current path is It writes the file inside the App Bundle.

    • If I run the executable inside the bundle from console, like, the current path is where the is.

    I don't want to define a fixed path when I run QFile::open(). How to make sure that the app always write in the folder where it is?

    Thanks in advance.

  • Lifetime Qt Champion


    Technically what you are trying to do is not clean. If you install your software on Windows (in Program Files), then it will also try to write to a read-only location and will fail. You should rather use QStandardPaths to retrieve one of the known path to store application data. On OS X, writing files in the bundle from your application is also not something that you should do.

    If you still want to do that, QCoreApplication::applicationDirPath is what you are looking for, but again, you should not

  • Hi SGaist, your suggestion works, Thank you. Despite your advice, I think an App should write all its small configuration files along with its executable. For the case of user data, your are right - it should save the data in a known system wide folder.

  • Lifetime Qt Champion

    Then you are going against Linux, OS X and Windows design guidelines at least.

    Have a look at QStandardPaths::StandardLocation more specifically the ConfigLocation key.

    Also, for application settings your have QSettings that handles the OS specific implementations for you.

  • You are right. Sorry about it. I didn't know the QSettings class. It seems to be what I need. I will follow your advice. Thank you.

  • Lifetime Qt Champion

    You're welcome !

    Since you have everything you need, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

  • Done! Thank you one time more.

Log in to reply