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. Accessing files in app - SOLVED
QtWS25 Last Chance

Accessing files in app - SOLVED

Scheduled Pinned Locked Moved General and Desktop
qtcreatorqfilemac
10 Posts 3 Posters 3.6k 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.
  • R Offline
    R Offline
    Rida
    wrote on last edited by Rida
    #1

    I'm building a desktop app in Qtcreator on a mac (OSX10.9)
    The app opens and loads some data from a few files, which can then later be used and changed by the program.
    This is all working fine as long as I launch the program from Qtcreator. However, if I launch the program by opening it in finder, it is not able to open any of the files (I know this because I've made some pop-up's appear any time a file can not be accessed).
    The files are stored in the standard directory for mac (Contents/MacOS) and as I said, everything works fine if I run it from QTcreator.
    Any advice to solve this issue is much appreciated.

    R 1 Reply Last reply
    0
    • R Rida

      I'm building a desktop app in Qtcreator on a mac (OSX10.9)
      The app opens and loads some data from a few files, which can then later be used and changed by the program.
      This is all working fine as long as I launch the program from Qtcreator. However, if I launch the program by opening it in finder, it is not able to open any of the files (I know this because I've made some pop-up's appear any time a file can not be accessed).
      The files are stored in the standard directory for mac (Contents/MacOS) and as I said, everything works fine if I run it from QTcreator.
      Any advice to solve this issue is much appreciated.

      R Offline
      R Offline
      raf924
      wrote on last edited by
      #2

      @Rida Post the code that accesses the files please. I think it might be a path problem but I can't be sure until I've seen the code.

      R 1 Reply Last reply
      0
      • R raf924

        @Rida Post the code that accesses the files please. I think it might be a path problem but I can't be sure until I've seen the code.

        R Offline
        R Offline
        Rida
        wrote on last edited by
        #3

        @raf924
        I'm quite sure that if the path was the problem the file could never be accessed.
        Or maybe there is something obvious that I'm missing.
        This code works fine if I compile it and run it in QtCreator, but as I said before it doesn't if I just run the program that is created by double-clicking it in finder.

        QList<FlightDay> readDayFile()
        {
            QList<FlightDay> newDaylist;
        
            QMessageBox warningbox(QMessageBox::Warning,"Warning","Could not open days.dat!",QMessageBox::Ok);
            
            QFile dayfile("days.dat");
            QString line;
            QStringList data;
            int datalen = 0;
            
            if(!dayfile.open(QIODevice::ReadOnly))
            {
                warningbox.exec();
            }
            else
            {
        
                QTextStream in(&dayfile);
                line = in.readLine();
                data = line.split(";");
                datalen = data.at(1).toInt();
                int i = 0;
        
                while(!in.atEnd())
                {
        
                    if(i < datalen)
                    {
                        line = in.readLine();
                        data = line.split(";");
                        newDaylist.append(FlightDay::FlightDay(QDate::fromString(data[2]),data[1],data[0].toInt()));
                        i++;
                    }
                    else
                    {
                        warningbox.setText("file error");
                        warningbox.exec();
                        newDaylist.clear();
                        break;
                    }
                }
            }
            return newDaylist;
        } ```
        R 1 Reply Last reply
        0
        • R Rida

          @raf924
          I'm quite sure that if the path was the problem the file could never be accessed.
          Or maybe there is something obvious that I'm missing.
          This code works fine if I compile it and run it in QtCreator, but as I said before it doesn't if I just run the program that is created by double-clicking it in finder.

          QList<FlightDay> readDayFile()
          {
              QList<FlightDay> newDaylist;
          
              QMessageBox warningbox(QMessageBox::Warning,"Warning","Could not open days.dat!",QMessageBox::Ok);
              
              QFile dayfile("days.dat");
              QString line;
              QStringList data;
              int datalen = 0;
              
              if(!dayfile.open(QIODevice::ReadOnly))
              {
                  warningbox.exec();
              }
              else
              {
          
                  QTextStream in(&dayfile);
                  line = in.readLine();
                  data = line.split(";");
                  datalen = data.at(1).toInt();
                  int i = 0;
          
                  while(!in.atEnd())
                  {
          
                      if(i < datalen)
                      {
                          line = in.readLine();
                          data = line.split(";");
                          newDaylist.append(FlightDay::FlightDay(QDate::fromString(data[2]),data[1],data[0].toInt()));
                          i++;
                      }
                      else
                      {
                          warningbox.setText("file error");
                          warningbox.exec();
                          newDaylist.clear();
                          break;
                      }
                  }
              }
              return newDaylist;
          } ```
          R Offline
          R Offline
          raf924
          wrote on last edited by
          #4

          @Rida I just took a look a the documentation for QFile::open and my guess is that you might have forgotten to copy days.dat next to the executable. It works in QtCreator because the working directory is different:
          QtCreator : your-app-build/
          Finder: your-app-build/debug
          Although now I see that you're using MacOS, my bad I don't develop on Mac but I think my suggestion is still valid.

          R 1 Reply Last reply
          0
          • R raf924

            @Rida I just took a look a the documentation for QFile::open and my guess is that you might have forgotten to copy days.dat next to the executable. It works in QtCreator because the working directory is different:
            QtCreator : your-app-build/
            Finder: your-app-build/debug
            Although now I see that you're using MacOS, my bad I don't develop on Mac but I think my suggestion is still valid.

            R Offline
            R Offline
            Rida
            wrote on last edited by
            #5

            @raf924

            I have done a lot of google-ing in the last few hours and I've found that the issue might be that I need to 'define' the files as resources in the projectfile. The explanation I've found is quite vague though. Can anyone point me in the right direction here?

            R 1 Reply Last reply
            0
            • R Rida

              @raf924

              I have done a lot of google-ing in the last few hours and I've found that the issue might be that I need to 'define' the files as resources in the projectfile. The explanation I've found is quite vague though. Can anyone point me in the right direction here?

              R Offline
              R Offline
              raf924
              wrote on last edited by raf924
              #6

              @Rida Ah you're trying to access a ressource file!! Okay I can help you. In QtCreator right-click your project, click Add New, go in the Qt tab and select "Qt ressource file". When the file is created right-click it and select "add exisiting files" and select the files you want to include inside your program. Finally, when you want to access those files, use

              QFile resource(":/filename");
              

              where filename is the name of the resource. For example

              QFile dayfile(":/days.dat");
              
              1 Reply Last reply
              0
              • R Offline
                R Offline
                Rida
                wrote on last edited by
                #7

                I'm afraid that's not exactly what I'm looking for.
                Here's a link to a thread on stackoverflow of a user with a similar problem

                http://stackoverflow.com/questions/24382331/packaging-mp3-files-into-qt-app

                He solved it by modifying the .pro file
                I tried something similar but all I got was a symbols not found for architecture error.

                I'm not entirely sure what he's doing there so if someone could explain this, I might be able to make sense of it and find out what I need to do to solve my issue.

                R 1 Reply Last reply
                0
                • R Rida

                  I'm afraid that's not exactly what I'm looking for.
                  Here's a link to a thread on stackoverflow of a user with a similar problem

                  http://stackoverflow.com/questions/24382331/packaging-mp3-files-into-qt-app

                  He solved it by modifying the .pro file
                  I tried something similar but all I got was a symbols not found for architecture error.

                  I'm not entirely sure what he's doing there so if someone could explain this, I might be able to make sense of it and find out what I need to do to solve my issue.

                  R Offline
                  R Offline
                  raf924
                  wrote on last edited by
                  #8

                  @Rida You added to you pro:

                  macx{
                   DAYDAT.files = ./day.dat
                   DAYDAT.path = Contents/Resources
                   QMAKE_BUNDLE_DATA += DAYDAT
                  }
                  

                  right?
                  I don't know how to access it though you'll have to play with the QCore::applicationDirPath() method to find the Resources folder

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    AnantAgrawal
                    wrote on last edited by
                    #9

                    Hi Rida,

                    My understanding from the above conversation is that you want to add some resource files into your application and then perform some operation on them. For the same, you don't have to manually copy the resource files into your installation directory. The resource file(s) will get statically linked to your application itself.

                    To make your application work, just replace QFile dayfile("days.dat"); with QFile dayfile(":/days.dat");.

                    Do make sure to clean-up your pro file and re-run qmake before making above changes.

                    Thanks,
                    Anant Agrawal

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      Rida
                      wrote on last edited by
                      #10

                      Thank you very much for all the advice.
                      If anyone encounters a similar issue, I managed to solve the issue in this manner.

                      No changes to the '.pro' file.
                      using this code to reference the file-path.

                      QFile dayfile(QCoreApplication::applicationDirPath()+"/days.dat")
                      

                      again thank you for pointing me in the right direction!

                      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