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. Path of Application Bundle - Solved

Path of Application Bundle - Solved

Scheduled Pinned Locked Moved General and Desktop
13 Posts 3 Posters 11.1k Views
  • 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.
  • B Offline
    B Offline
    blissful
    wrote on last edited by
    #1

    Hi
    I am using a API that requires the path (as a char *) of where the file concerned is located to be passed into a method as an argument.
    e.g.
    nameOfMethod(Path of where the file is located in char * form);

    The file is in a subfolder called "Other Files" inside the same folder as where the .pro file is. The "Other Files" folder was automatically created by Qt after I added the existing file...
    So, it is in the "application" folder I guess...I don't know what the term in Qt is for this.

    Here is my current code that does not achieve the result that I want:
    QString stringPath = QDir::currentPath();
    QByteArray ba = stringPath.toLocal8Bit();
    char * c_str2 = ba.data();

    swe_set_ephe_path(c_str2);
    

    In iOS I have gotten it to work. I am basically trying to find the Qt equivalent of the code below.
    NSBundle *mainBundle = [NSBundle mainBundle];

    NSString resourcePath = [mainBundle resourcePath];
    swe_set_ephe_path((char
    )[resourcePath UTF8String]);

    How do I get the path for the Main Bundle (Application folder) in Qt?

    Are folders in Qt project, real folders or are they there just to give a appearance of structure?
    Like in Xcode(iOS), the files inside your projects could appear to be in different folders, but they only give the appearance of structure and they are still access through the main bundle...no need for mainbundle/subfolder/anothersubfolder/ etc....
    ?

    Thank you...

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

      Hi,

      IIRC "QCoreApplication::applicationDirPath":http://qt-project.org/doc/qt-4.8/qcoreapplication.html#applicationDirPath is what you are looking for.

      The resource folder should be applicationDirPath + "/../Resources".

      As for QtCreator, the Projects view creates a structure to show you i.e the headers and implementation files separately but does not reflect the current state of the underlying filesystem.

      Hope it helps

      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
      • T Offline
        T Offline
        tobias.hunger
        wrote on last edited by
        #3

        The "Projects" view in Qt Creator visualizes the build system structure and is not necessarily related to the file system. Please use the Filesystem view to check the actual structure of the file system.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          blissful
          wrote on last edited by
          #4

          Hi
          I've added the file to the Resources by "Adding New Resource File".
          I called my .qrc file MyResources.qrc
          For the prefix, I put "files".

          I've tried:
          QString stringPath = QCoreApplication::applicationFilePath();
          stringPath = stringPath + "/../Resources/files/";

          and

          QString stringPath = QCoreApplication::applicationFilePath();
          stringPath = stringPath + "/Resources/files/";

          Both are not working for me.

          Am I missing something?

          Thank you in advance...

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

            Yes you are,

            The Resources from Qt are not the same thing as the Resources folder from an app bundle on OS X.

            Read Qt's "resources documentation":http://qt-project.org/doc/qt-4.8/resources.html to have a better understanding of how to use them

            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
            • B Offline
              B Offline
              blissful
              wrote on last edited by
              #6

              Hi
              Ok, I just read it....I'm starting to think that using the Qt resource system (.qrc) is not the way to go....
              In there it says:
              "The Qt resource system is a platform-independent mechanism for storing binary files in the application's executable"

              Basically I have a function
              swe_set_ephe_path(char * parameter)
              that takes in a char * as a parameter.
              The char * parameter should be the path to the directory where I store this file called "fixstars.cat".
              I think it should be able to find the "fixstars.cat" file as it is and not as a binary file.

              I'm using a 3rd party API. To be able to use it properly for me, I should tell the API in which directory to find fixstars.cat, by passing in the path to the swe_set_ephe_path(char * parameter) function.

              How can I achieve this in Qt?

              Thank you very much for the help.

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

                Something like ?

                @QByteArray local8Bit = stringPath.toLocal8Bit();
                swe_set_ephe_path(local8Bit.data());@

                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
                • B Offline
                  B Offline
                  blissful
                  wrote on last edited by
                  #8

                  Hi
                  Actually, it is finding the right path to convert to char * that is my problem.
                  Here is my code:

                  @QString stringPath = QCoreApplication::applicationFilePath();

                  stringPath = stringPath + "/Resources/files/";
                  
                  qDebug() << stringPath;
                  
                  QByteArray ba = stringPath.toLocal8Bit();
                  char * c_str2 = ba.data();
                  
                  swe_set_ephe_path(c_str2);
                  
                  
                  double x[6];
                  char serr[365];
                  long ret_flag = swe_fixstar_ut("Spica", 300, SEFLG_NONUT, x, serr);
                  
                  qDebug() << ret_flag;
                  

                  @

                  ret_flag prints out in the console as -1.
                  Meaning to say, i didn't enter the correct path to where fixstars.cat is.
                  I have added fixstars.cat to a .qrc file with "files" as the prefix.

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

                    A .qrc resource is embedded in your binary not in your app bundle. Copy your fixstars.cat in your bundle or copy it from your resources to a temporary file and give the path to that temporary file to swe_set_ephe_path.

                    Anyway, you can't give a Qt resources path to a framework that doesn't support that, it won't be able to read 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
                    • B Offline
                      B Offline
                      blissful
                      wrote on last edited by
                      #10

                      Hi
                      Ok. That is very good advice. Thank you.
                      I've been trying to go that route now.
                      I have been experimenting with the code to write to file:
                      @void MainWindow::write()
                      {
                      QFile file(QCoreApplication::applicationDirPath() + "/out.txt");
                      if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
                      {
                      qDebug() << "CANNOT OPEN";
                      return;
                      }

                           qDebug() << "FILE OPENED";
                           QTextStream out(&file);
                           out << "The magic number is: " << 49 << "\n";
                      
                           file.flush();
                           file.close();
                      

                      }@

                      I get the "FILE OPENED" string printed out to the console.
                      I would think that "out.txt" file would be created inside the folder where the executable for this app is. I go there and don't see any file.
                      I then search for out.txt in the Finder Window (I'm using a mac) and can't find any out.txt inside my comp.

                      What am I doing incorrectly?
                      Howcome I can't write to that file.
                      I haven't created the out.txt, but based on some tutorials i've seen and read, i think the file should be created automatically by Qt if it doesn't exist....(I might be wrong, but that's what I think...)...

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

                        I think you are currently getting to fast.

                        First thing to do is to check what file path you have i.e
                        @qDebug() << file.fileName();@
                        That will tell you where it is located.

                        It should be created indeed. I don't know what is going wrong.

                        In any case, i would suggest you to start simple:
                        Put your fixstars.cat file in known location (i.e. Documents), try to load it from there using Qt. Once that work, move it in your bundle Resources/files subdirectory. Load it from there.

                        Before embedding the file in you binary, answer these questions: Is it subject to change ? If so, how often ? Is it then really worth to put it in the binary rather than the bundle ?

                        Hope it helps

                        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
                        • B Offline
                          B Offline
                          blissful
                          wrote on last edited by
                          #12

                          Hi
                          Thank you so much :)
                          I've managed to write the code for what I want:

                          @
                          void MainWindow::read()
                          {
                          QFile mFile("/Users/bliss/Documents/fixstars.cat");

                          if(!mFile.open(QFile::ReadOnly | QFile::Text))
                          {
                              qDebug() << "could not open file for reading";
                              return;
                          }
                          
                          QTextStream in(&mFile);
                          QString mText = in.readAll();
                          
                          qDebug() << mText;
                          
                          
                          
                          mFile.flush();
                          mFile.close();
                          

                          }

                          void MainWindow::readFromResource()
                          {
                          QFile mFile(":/files/fixstars.cat");

                          if(!mFile.open(QFile::ReadOnly | QFile::Text))
                          {
                              qDebug() << "could not open file for reading";
                              return;
                          }
                          
                          QTextStream in(&mFile);
                          QString mText = in.readAll();
                          
                          qDebug() << mText;
                          
                          
                          qDebug() << "FROM readFromResource()";
                          
                          mFile.flush();
                          mFile.close();
                          

                          }

                          void MainWindow::readFromResourceWriteToApplicationDir()
                          {
                          QFile mFile(":/files/fixstars.cat");

                          if(!mFile.open(QFile::ReadOnly | QFile::Text))
                          {
                              qDebug() << "could not open file for reading";
                              return;
                          }
                          
                          QTextStream in(&mFile);
                          QString mText = in.readAll();
                          
                          
                          QFile file&#40;QCoreApplication::applicationDirPath(&#41; + "/fixstars.cat");
                          
                          
                               if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
                               {
                                   qDebug() << "CANNOT OPEN";
                                   return;
                               }
                          
                               qDebug() << "FILE OPENED";
                               QTextStream out(&file);
                               out << mText;
                          
                          
                               qDebug() << file.fileName();
                          
                               file.flush();
                               file.close();
                          

                          }@

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

                            You're welcome !

                            You don't need the QTextStream if you are reading the complete file anyway, QFile also has a readAll.

                            Don't forget to update the thread's title to solved so other forum users may know that a solution has been found :)

                            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