How to get current working directory in a Qt application?



  • I'm writing a program in C++ using the Qt library. I would like to get current working directory of my program (I mean that is the absolute path which contains file .pro). I saw the QDir::currentPath() function, but it gives back the directory where the binary is. I don’t get the directory that contains the application executable.

    !http://i249.photobucket.com/albums/gg220/HienBoo/qt.png(Get current working direction in QT Application)!

    Does anyone has any solutions?

    Thank you,
    Lee.

    P/S: I have uploaded the image which describes the path that I want to get.



  • Hi LeeMinh.
    Did you try "QCoreApplication::applicationDirPath()":http://qt-project.org/doc/qt-5.0/qtcore/qcoreapplication.html#applicationDirPath
    From doc:

    bq. Returns the directory that contains the application executable.

    Regards.



  • The return value of
    @
    QDir::currentPath()
    @

    should be you current working directory.

    It equals your applicationDir or not depending on yours.


  • Moderators

    welcome to devnet

    If you are starting your application in qt creator it has typically the folder of your binary as working folder or one folder below, IIRC. You can set the actual working in Qt creator in projects under the run tab. This entry points to the working directory when starting the application.



  • I tried this function but this function gave back the directory where the binary is. I just wanna get current working directory, I don't get the directory that contains the application executable.
    [quote author="1+1=2" date="1373721933"]The return value of
    @
    QDir::currentPath()
    @

    should be you current working directory.

    It equals your applicationDir or not depending on yours.

    [/quote]



  • Hi, seems you have misunderstand the meaning of current working directory.

    If you don't change the CWD in your application's source code, it's the directory in which your application get start. It have nothing to do with nether your source code directory nor binary directory.

    BTW, CWD can be any directory of your OS.


Log in to reply
 

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