Cross-platform way to change working program directory...



  • Hello,

    I have the following code which changes the working directory of the program to where the program is located. I do this so that my application can correctly find all necessary resources needed to run.

    My question is, what is the cross-platform way to do this in Qt?

    Thank you for your time.

    This is in the int main() function:

    @
    /*

    Change current directory of program to where executable is.
    
    */
    
    std::string ProgramPath(argv[0]);
    std::string WorkingDirectory;
    
    size_t found;
    found = ProgramPath.find_last_of("/\\");
    WorkingDirectory = ProgramPath.substr(0, found);
    

    #if defined(_WIN32)

    _chdir(WorkingDirectory.c_str());
    

    #endif

    #if defined(gnu_linux)

    chdir(WorkingDirectory.c_str());
    

    #endif

    /*
    
    End change of working directory.
    
    */
    

    @


  • Moderators

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



  • This works! Thank you for your time.


Log in to reply
 

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