Important: Please read the Qt Code of Conduct -

Accessing files in app - SOLVED

  • 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.

  • @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.

  • @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;
            QTextStream in(&dayfile);
            line = in.readLine();
            data = line.split(";");
            datalen =;
            int i = 0;
                if(i < datalen)
                    line = in.readLine();
                    data = line.split(";");
                    warningbox.setText("file error");
        return newDaylist;
    } ```

  • @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.

  • @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?

  • @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");

  • 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

    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.

  • @Rida You added to you pro:

     DAYDAT.files = ./day.dat
     DAYDAT.path = Contents/Resources

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

  • 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.

    Anant Agrawal

  • 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!

Log in to reply