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. No wanting re-event the wheel, is there already a function to do this?
Qt 6.11 is out! See what's new in the release blog

No wanting re-event the wheel, is there already a function to do this?

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 6 Posters 3.5k 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    When I build an application on the iMAC using Qt Creator the app is actually a folder with the following structure:

    Contents     (folder)
      MacOS      (folder)
        application executable file here
     info.plist
     PkgInfo
     Resources.  (folder)
      empty.lproj
    

    When calling:

    QCoreApplication::applicationFilePath()
    

    The actual path returned contains now everything including the path of the compressed above structure:

    /Users/sy/XMLMPAM/build-mdFileIO-Desktop_Qt_5_15_2_clang_64bit-Debug/mdFileIO.app/Contents/MacOS/mdFileIO
    

    Where in the above path the file created by Qt Creator is mdFileIO.app. I have written a routine which returns just the file path to the application:

        const quint8 cuint8Delimiters = 3;
    
        QString strAppName = QCoreApplication::applicationFilePath(), strPath;
    
        if ( strAppName.isEmpty() == true ) {
        //Cannot do anything without application path and name
            return;
        }
        //Locate last delimiter in path
        int intIdx = strAppName.lastIndexOf(QDir::separator());
    
        if ( intIdx == -1 ) {
        //Cannot locate path seperator!
            return;
        }
        //Go back to the location of the actual file path
        quint8 uint8Found = 0;
        for( int i=intIdx-1; i>=0; i-- ) {
            if ( strAppName[i] == QDir::separator() ) {
                if ( ++uint8Found == cuint8Delimiters ) {
        //Extract just the path
                    strPath = strAppName.mid(0, i);
                    break;
                }
            }
        }
        if ( strPath.isEmpty() == true ) {
        //Path not found        
            return;
        }
    

    Is there something that already exists to do the same?

    Kind Regards,
    Sy

    J.HilkJ KroMignonK 2 Replies Last reply
    0
    • SPlattenS SPlatten

      When I build an application on the iMAC using Qt Creator the app is actually a folder with the following structure:

      Contents     (folder)
        MacOS      (folder)
          application executable file here
       info.plist
       PkgInfo
       Resources.  (folder)
        empty.lproj
      

      When calling:

      QCoreApplication::applicationFilePath()
      

      The actual path returned contains now everything including the path of the compressed above structure:

      /Users/sy/XMLMPAM/build-mdFileIO-Desktop_Qt_5_15_2_clang_64bit-Debug/mdFileIO.app/Contents/MacOS/mdFileIO
      

      Where in the above path the file created by Qt Creator is mdFileIO.app. I have written a routine which returns just the file path to the application:

          const quint8 cuint8Delimiters = 3;
      
          QString strAppName = QCoreApplication::applicationFilePath(), strPath;
      
          if ( strAppName.isEmpty() == true ) {
          //Cannot do anything without application path and name
              return;
          }
          //Locate last delimiter in path
          int intIdx = strAppName.lastIndexOf(QDir::separator());
      
          if ( intIdx == -1 ) {
          //Cannot locate path seperator!
              return;
          }
          //Go back to the location of the actual file path
          quint8 uint8Found = 0;
          for( int i=intIdx-1; i>=0; i-- ) {
              if ( strAppName[i] == QDir::separator() ) {
                  if ( ++uint8Found == cuint8Delimiters ) {
          //Extract just the path
                      strPath = strAppName.mid(0, i);
                      break;
                  }
              }
          }
          if ( strPath.isEmpty() == true ) {
          //Path not found        
              return;
          }
      

      Is there something that already exists to do the same?

      J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @SPlatten I don't quite follow,

      you want the following returned ?

      /Users/sy/XMLMPAM/build-mdFileIO-Desktop_Qt_5_15_2_clang_64bit-Debug/mdFileIO.app/Contents/MacOS/mdFileIO
      

      ? than you can use QCoreApplication::applicationDirPath() for that,
      if you want only the path of the app bundle, I'm using the following function myself:

      static const QString &ApplicationFolder (){
      #ifdef Q_OS_MACOS
              auto getStringPath = []()->QString{
                      CFURLRef url =(CFURLRef)CFAutorelease((CFURLRef)CFBundleCopyBundleURL(CFBundleGetMainBundle()));
                      QDir d(QUrl::fromCFURL(url).path());
                      d.cdUp();
                      return  d.absolutePath();
              };
              static const QString path = getStringPath();
      #else
              static const QString path = QCoreApplication::applicationDirPath();
      #endif
              return  path;
          }
      

      Not sure if there exists something for that in Qt, I never really found anything 🤷‍♂️


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      SPlattenS 1 Reply Last reply
      1
      • SPlattenS SPlatten

        When I build an application on the iMAC using Qt Creator the app is actually a folder with the following structure:

        Contents     (folder)
          MacOS      (folder)
            application executable file here
         info.plist
         PkgInfo
         Resources.  (folder)
          empty.lproj
        

        When calling:

        QCoreApplication::applicationFilePath()
        

        The actual path returned contains now everything including the path of the compressed above structure:

        /Users/sy/XMLMPAM/build-mdFileIO-Desktop_Qt_5_15_2_clang_64bit-Debug/mdFileIO.app/Contents/MacOS/mdFileIO
        

        Where in the above path the file created by Qt Creator is mdFileIO.app. I have written a routine which returns just the file path to the application:

            const quint8 cuint8Delimiters = 3;
        
            QString strAppName = QCoreApplication::applicationFilePath(), strPath;
        
            if ( strAppName.isEmpty() == true ) {
            //Cannot do anything without application path and name
                return;
            }
            //Locate last delimiter in path
            int intIdx = strAppName.lastIndexOf(QDir::separator());
        
            if ( intIdx == -1 ) {
            //Cannot locate path seperator!
                return;
            }
            //Go back to the location of the actual file path
            quint8 uint8Found = 0;
            for( int i=intIdx-1; i>=0; i-- ) {
                if ( strAppName[i] == QDir::separator() ) {
                    if ( ++uint8Found == cuint8Delimiters ) {
            //Extract just the path
                        strPath = strAppName.mid(0, i);
                        break;
                    }
                }
            }
            if ( strPath.isEmpty() == true ) {
            //Path not found        
                return;
            }
        

        Is there something that already exists to do the same?

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #3

        @SPlatten said in No wanting re-event the wheel, is there already a function to do this?:

        Is there something that already exists to do the same?

        Sometimes reading doc can help:
        QString QCoreApplication::applicationDirPath()

        Returns the directory that contains the application executable.
        For example, if you have installed Qt in the C:\Qt directory, and you run the regexp example, this function will return "C:/Qt/examples/tools/regexp".
        On macOS and iOS this will point to the directory actually containing the executable, which may be inside an application bundle (if the application is bundled).

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        SPlattenS 2 Replies Last reply
        1
        • J.HilkJ J.Hilk

          @SPlatten I don't quite follow,

          you want the following returned ?

          /Users/sy/XMLMPAM/build-mdFileIO-Desktop_Qt_5_15_2_clang_64bit-Debug/mdFileIO.app/Contents/MacOS/mdFileIO
          

          ? than you can use QCoreApplication::applicationDirPath() for that,
          if you want only the path of the app bundle, I'm using the following function myself:

          static const QString &ApplicationFolder (){
          #ifdef Q_OS_MACOS
                  auto getStringPath = []()->QString{
                          CFURLRef url =(CFURLRef)CFAutorelease((CFURLRef)CFBundleCopyBundleURL(CFBundleGetMainBundle()));
                          QDir d(QUrl::fromCFURL(url).path());
                          d.cdUp();
                          return  d.absolutePath();
                  };
                  static const QString path = getStringPath();
          #else
                  static const QString path = QCoreApplication::applicationDirPath();
          #endif
                  return  path;
              }
          

          Not sure if there exists something for that in Qt, I never really found anything 🤷‍♂️

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #4

          @J-Hilk , What I want returned is:

          /Users/sy/XMLMPAM/build-mdFileIO-Desktop_Qt_5_15_2_clang_64bit-Debug/
          

          Kind Regards,
          Sy

          J.HilkJ 1 Reply Last reply
          0
          • KroMignonK KroMignon

            @SPlatten said in No wanting re-event the wheel, is there already a function to do this?:

            Is there something that already exists to do the same?

            Sometimes reading doc can help:
            QString QCoreApplication::applicationDirPath()

            Returns the directory that contains the application executable.
            For example, if you have installed Qt in the C:\Qt directory, and you run the regexp example, this function will return "C:/Qt/examples/tools/regexp".
            On macOS and iOS this will point to the directory actually containing the executable, which may be inside an application bundle (if the application is bundled).

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #5

            @KroMignon , obviously I'm just impatient, thanks for the information.

            Kind Regards,
            Sy

            1 Reply Last reply
            0
            • SPlattenS SPlatten

              @J-Hilk , What I want returned is:

              /Users/sy/XMLMPAM/build-mdFileIO-Desktop_Qt_5_15_2_clang_64bit-Debug/
              
              J.HilkJ Online
              J.HilkJ Online
              J.Hilk
              Moderators
              wrote on last edited by J.Hilk
              #6

              @SPlatten yeah, thats exactly the path the native function call I posted should return


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0
              • KroMignonK KroMignon

                @SPlatten said in No wanting re-event the wheel, is there already a function to do this?:

                Is there something that already exists to do the same?

                Sometimes reading doc can help:
                QString QCoreApplication::applicationDirPath()

                Returns the directory that contains the application executable.
                For example, if you have installed Qt in the C:\Qt directory, and you run the regexp example, this function will return "C:/Qt/examples/tools/regexp".
                On macOS and iOS this will point to the directory actually containing the executable, which may be inside an application bundle (if the application is bundled).

                SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by
                #7

                @KroMignon , actually, having just tried it, its not what I'm after, calling:

                QCoreApplication::applicationDirPath();
                

                Gives:

                /Users/sy/XMLMPAM/build-mdFileIO-Desktop_Qt_5_15_2_clang_64bit-Debug/mdFileIO.app/Contents/MacOS
                

                What I want is:

                /Users/sy/XMLMPAM/build-mdFileIO-Desktop_Qt_5_15_2_clang_64bit-Debug/
                

                Kind Regards,
                Sy

                KroMignonK 1 Reply Last reply
                0
                • SPlattenS SPlatten

                  @KroMignon , actually, having just tried it, its not what I'm after, calling:

                  QCoreApplication::applicationDirPath();
                  

                  Gives:

                  /Users/sy/XMLMPAM/build-mdFileIO-Desktop_Qt_5_15_2_clang_64bit-Debug/mdFileIO.app/Contents/MacOS
                  

                  What I want is:

                  /Users/sy/XMLMPAM/build-mdFileIO-Desktop_Qt_5_15_2_clang_64bit-Debug/
                  
                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by
                  #8

                  @SPlatten said in No wanting re-event the wheel, is there already a function to do this?:

                  What I want is:

                  Again, found in Qt documentation:
                  https://doc.qt.io/qt-5/macos-issues.html#macos-native-api-access

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  SPlattenS 1 Reply Last reply
                  1
                  • KroMignonK KroMignon

                    @SPlatten said in No wanting re-event the wheel, is there already a function to do this?:

                    What I want is:

                    Again, found in Qt documentation:
                    https://doc.qt.io/qt-5/macos-issues.html#macos-native-api-access

                    SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #9

                    @KroMignon, Thank you, does this mean that on different platforms (Linux and Windows) that the application is stored differently?

                    Kind Regards,
                    Sy

                    jsulmJ S 2 Replies Last reply
                    0
                    • SPlattenS SPlatten

                      @KroMignon, Thank you, does this mean that on different platforms (Linux and Windows) that the application is stored differently?

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @SPlatten said in No wanting re-event the wheel, is there already a function to do this?:

                      does this mean that on different platforms (Linux and Windows) that the application is stored differently?

                      Yes, these are very different operating systems.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      SPlattenS 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @SPlatten said in No wanting re-event the wheel, is there already a function to do this?:

                        does this mean that on different platforms (Linux and Windows) that the application is stored differently?

                        Yes, these are very different operating systems.

                        SPlattenS Offline
                        SPlattenS Offline
                        SPlatten
                        wrote on last edited by
                        #11

                        @jsulm , actually Linux is very similar at the command line level with macOS.

                        Kind Regards,
                        Sy

                        jsulmJ 1 Reply Last reply
                        0
                        • SPlattenS SPlatten

                          @jsulm , actually Linux is very similar at the command line level with macOS.

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @SPlatten said in No wanting re-event the wheel, is there already a function to do this?:

                          actually Linux is very similar at the command line level with macOS

                          But not if it comes to packaging software...

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          SPlattenS 1 Reply Last reply
                          1
                          • jsulmJ jsulm

                            @SPlatten said in No wanting re-event the wheel, is there already a function to do this?:

                            actually Linux is very similar at the command line level with macOS

                            But not if it comes to packaging software...

                            SPlattenS Offline
                            SPlattenS Offline
                            SPlatten
                            wrote on last edited by SPlatten
                            #13

                            @jsulm , so I guess what I want now is a multipatform fool proof way of getting the application path not including any OS specific additions.

                            Kind Regards,
                            Sy

                            KroMignonK 1 Reply Last reply
                            0
                            • SPlattenS SPlatten

                              @jsulm , so I guess what I want now is a multipatform fool proof way of getting the application path not including any OS specific additions.

                              KroMignonK Offline
                              KroMignonK Offline
                              KroMignon
                              wrote on last edited by
                              #14

                              @SPlatten said in No wanting re-event the wheel, is there already a function to do this?:

                              so I guess what I want now is a multipatform fool proof way of getting the application path not including any OS specific additions.

                              QString QCoreApplication::applicationDirPath()
                              Returns the directory that contains the application executable.
                              For example, if you have installed Qt in the C:\Qt directory, and you run the regexp example, this function will return "C:/Qt/examples/tools/regexp".
                              On macOS and iOS this will point to the directory actually containing the executable, which may be inside an application bundle (if the application is bundled).

                              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

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

                                Hi,

                                That's why it's usual to have an #ifdef Q_OS_MACOS which contains the code to move up two folders from what is returned by QCoreApplication::applicationDirPath().

                                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
                                2
                                • SPlattenS SPlatten

                                  @KroMignon, Thank you, does this mean that on different platforms (Linux and Windows) that the application is stored differently?

                                  S Offline
                                  S Offline
                                  SimonSchroeder
                                  wrote on last edited by
                                  #16

                                  @SPlatten said in No wanting re-event the wheel, is there already a function to do this?:

                                  @KroMignon, Thank you, does this mean that on different platforms (Linux and Windows) that the application is stored differently?

                                  Yes, application bundles is specific to macOS. In a sense QCoreApplication::applicationDirPath() does the right thing because an app bundle might also have some additional executables in a different subfolder. applicationDirPath() would return these for those additional executables instead.

                                  To my knowledge Windows does not have anything comparable. So, on Windows applicationDirPath() will always return the correct path.

                                  Linux in general behaves the same as Windows in this respect. However, we started using AppImage to bundle our application for Linux. It is similar to macOS' app bundle, but it uses a disk image instead of a folder. Running the application mounts the image to a temporary directory (which is not a subdirectory of where the actual AppImage is located). There is an environment variable to get the directory of the AppImage. linuxdeployqt (the equivalent to macdeployqt) will generate such an AppImage.

                                  So, in summary only on Windows does applicationDirPath() give you what you want (also on Linux if you don't create an AppImage). Everything else you need to handle by yourself. I don't know of any library - let alone one wildly used - which would do exactly that.

                                  1 Reply Last reply
                                  3

                                  • Login

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