Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Application bundle path

Application bundle path

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.1k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • PerdrixP Offline
    PerdrixP Offline
    Perdrix
    wrote on last edited by
    #1

    The Qt docs say:
    Accessing the Bundle Path

    macOS applications are structured as a directory (ending with .app). This directory contains sub-directories and files. It may be useful to place items, such as plugins and online documentation, inside this bundle. The following code returns the path of the application bundle:

     #ifdef Q_OS_MAC
         QString bundlePath = QString::fromNSString(NSBundle.mainBundle.bundlePath);
         qDebug() << "Bundle path =" << bundlePath;
     #endif
    

    but as that stands I get:

    /Users/amonra/.vs/DSS/DeepSkyStacker/DeepSkyStacker.cpp:602:44: error: use of undeclared identifier 'NSBundle'
      602 |         QString bundlePath{ QString::fromNSString(NSBundle.mainBundle.bundlePath) };
    

    Clearly there's a header file I need to make that work - what do I need to include?

    Thanks
    David

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      NSBundle comes from the Foundation framework.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • PerdrixP Offline
        PerdrixP Offline
        Perdrix
        wrote on last edited by
        #3

        Err that doesn't answer my question :( That web link only talks about objective C - nothing about C++ in there.

        1 Reply Last reply
        0
        • I Online
          I Online
          IgKh
          wrote on last edited by
          #4

          Well, Cocoa APIs are in Objective-C so can be accessed only via that language, or something that bridges to it (like Swift).

          The example in the documentation seems wrong. It looks like Swift code, which never mixes with C++ in the same file.

          Your best best is to make the file compile as Objective-C++, by changing its extension to .mm. In such files you can freely mix C++ and Objective-C code, and the line will be something like:

          NSString* path = [[NSBundle mainBundle] bundlePath];
          QString bundlePath = QString::fromNSString(path);
          

          After #import-ing the Foundations library of course. The downside is that now that file compiles only on macOS with Xcode, so you probably want to have one/few files in your project only for specific Mac utility methods.

          There are also very low-level ways to send messages to Objective-C objects directly from C/C++ without compiling with the Objective-C compiler, but you probably don't want to go there.

          1 Reply Last reply
          0
          • PerdrixP Offline
            PerdrixP Offline
            Perdrix
            wrote on last edited by Perdrix
            #5

            OK I don't really wish to go there :(

            I suppose I could write a .mm "helper function" but in the end I gave up on the recommendation in the docs and used
            QString appPath{ QCoreApplication::applicationDirPath() };
            to get the location of the macOS subfolder and navigated from there.

            Is there a better way?

            SGaistS 1 Reply Last reply
            0
            • I Online
              I Online
              IgKh
              wrote on last edited by IgKh
              #6

              No, and it is very hard to make a good macOS Qt app without any Objective-C++ or Swift. You always end up needing something in AppKit or Foundations that Qt doesn’t abstract sooner or later. MacOS is just too different.

              1 Reply Last reply
              0
              • PerdrixP Perdrix

                OK I don't really wish to go there :(

                I suppose I could write a .mm "helper function" but in the end I gave up on the recommendation in the docs and used
                QString appPath{ QCoreApplication::applicationDirPath() };
                to get the location of the macOS subfolder and navigated from there.

                Is there a better way?

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Perdrix QStandardPaths with QStandardPaths::ApplicationsLocation might be what you are looking for.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • PerdrixP Offline
                  PerdrixP Offline
                  Perdrix
                  wrote on last edited by Perdrix
                  #8

                  @SGaist I fear not, that's just /Applications, whereas I need the location of the bundle the app is running from ...

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Sorry, I've misunderstood your goal. So QCoreApplication::applicationDirPath is the simple way to do it.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved