Get current working directory in a Qt application



  • I'm writing a program in C++ using the Qt library. There is a symbolic link in my home bin directory to the executable. I would like the current working directory of my program to be the directory in which I am with my terminal (ie. the result of the pwd command). I saw the QDir::currentPath() function, but it gives back the directory where the binary is.

    How can I find my current working directory?


  • Moderators

    qDebug() << QDir().absoluteDirPath();
    // Or, if you are inside an AppImage package:
    qDebug() << qgetenv("APPIMAGE");
    


  • @davidspen
    From exactly what you say in your question, I would not have answered it as @sierdzio has. (Unless there is something special about "if you are inside an AppImage package", I don't know about that.)

    How/where do you launch your Qt executable from?

    You say

    I would like the current working directory of my program to be the directory in which I am with my terminal (ie. the result of the pwd command).

    If you go into a terminal and type the name of an executable to run, which is then found via a symbolic link in your ~/bin which is on your PATH, that will not change the current directory. It will remain whatever it was in the terminal you launched it from. Which is what you say you want. And what QDir::currentPath() should already be delivering you:

    Returns the absolute path of the application's current directory. The current directory is the last directory set with QDir::setCurrent() or, if that was never called, the directory at which this application was started at by the parent process.

    In that case I would ask why it is not already correct?

    If you instead want something about the directory the executable is actually in, or you run it in a different way, that's a different question?


 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.