0

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.

p.i.g.
  • 2,815
  • 2
  • 24
  • 41
coucou8949
  • 41
  • 1
  • 7
  • 2
    `this should return the main executable directory`: This is not necessarily true. A user can run your application with any initial working directory. – Francis Gagné Jun 29 '15 at 22:02
  • i know that, and its true. except that right now, i m the only one running my app and -as i think- it should launch within its proper directory.still your comment had give me a hint about something else.i thank you. – coucou8949 Jun 29 '15 at 22:14
  • "it should launch within its proper directory" It you don't launch it from the command line, it should not. If you believe otherwise, point us to a spec that substantiates this belief. – Kuba hasn't forgotten Monica Jun 30 '15 at 23:38

2 Answers2

0

I tried with the below code and it works fine for several runs. The version details of Qt and OS may help.

Qt creator 3.3.2 (open source). Qt lib 5.4.1.Os windows 8.1

    #include <QCoreApplication>
#include "QDir"
#include "qDebug"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QDir oldDir=QDir::current();
    qDebug()<<QDir::current().absolutePath();
    QDir sess("H:\\cuteapps\\session");
    if(!oldDir.exists("H:\\cuteapps\\session"))//if "Sessions" Dir doesn't exist
         oldDir.mkdir("H:\\cuteapps\\session");//create it.
    QDir::setCurrent(sess.absolutePath());
    qDebug()<<QDir::current().absolutePath();
    return a.exec();
}

output 1:
"H:/cuteapps/build-untitled2-Desktop_Qt_5_4_1_MSVC2012_OpenGL_32bit-Debug"
"H:/cuteapps/session"

output 2:
"H:/cuteapps/build-untitled2-Desktop_Qt_5_4_1_MSVC2012_OpenGL_32bit-Debug"
"H:/cuteapps/session"

output 3:
"H:/cuteapps/build-untitled2-Desktop_Qt_5_4_1_MSVC2012_OpenGL_32bit-Debug"
"H:/cuteapps/session"
Jeet
  • 1,006
  • 1
  • 14
  • 25
  • what i want to know is : why the my "current()" program directory defaults in the second run to the "Sessions" directory that i created in the first run and not to the (restored) main executable directory. – coucou8949 Jun 29 '15 at 22:21
0

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.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313