Search code examples
c++qtqdir

Qt QDir::current()


I had some code like this:

void MainWindow::saveData()
{
    QDir oldDir=QDir::current();//this should return the main executable directory.Since there is no other place in my hole code where i temper with QDir.
    QDir sess("Sessions");
    if(!oldDir.exists("Sessions"))//if "Sessions" Dir doesn't exist 
        oldDir.mkdir("Sessions");//create it.
    QDir::setCurrent(sess.absolutePath());
    //some virtual code inside current Dir, which i didn't implement yet.
    QDir::setCurrent(oldDir.absolutePath());//restore old dir
}

When I run my app firstly the code works perfectly.but in the second run, the first call to QDir::current(); returns the "Sessions" Dir and not the main executable Dir as it should be restored in the first run.actually i did manage to overcome this by adding one line at the biginning of the code, the following :

QDir::setCurrent(QCoreApplication::applicationDirPath());

Still I want to know why the first code didn't work.already checked for the documentation of the functions and found nothing.


Solution

  • QDir::current();//this should return the main executable directory

    No it should not! Not unless you change it to point there first.

    I'm dead serious when I'll say this: Yours is a myth, fantasy, whatever you call it, I have no idea what gave you the idea. Point me to a spec that says so. Oh, you can't, because there's no such spec, no such requirement. It's someone's twilight hour mirage that seems to perpetuate itself endlessly. If you heard it from someone, you have every right to be angry at them this very moment, for they did you a big disservice.

    Generally speaking, for applications that are not normally started from the command line, the initial working directory can be anything and it will be platform- and session/system configuration dependent. For a typical GUI application, assuming any particular initial working directory is a fool's errand and completely misguided.

    Once you change it to where you want it to point to, you of course have full control over it, but the initial working directory must be assumed to be random and out of your control.

    For example, on Windows I can start your application through an Explorer shortcut where I can specify whatever startup folder I desire, and you have zero control over it. On OS X, Finder sets the working directory to something or another, IIRC to the folder where the app bundle resides. Again, you as a developer have no control over it unless there's some setting in the bundle that you could add to that effect, but that is platform-specific and will be ignored if your application is not started through Finder or bundle API mechanisms (they probably are called something else). And so on. It's completely arbitrary and it's pointless to depend on its initial value.

    If you want to refer to the application's installation directory or executable directory, do so explicitly. Do not assume anything about the initial working directory of a GUI application.