Get current working directory in a Qt application

后端 未结 5 1676
春和景丽
春和景丽 2020-12-04 23:13

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 pro

5条回答
  •  盖世英雄少女心
    2020-12-05 00:03

    I'm running Qt 5.5 under Windows and the default constructor of QDir appears to pick up the current working directory, not the application directory.

    I'm not sure if the getenv PWD will work cross-platform and I think it is set to the current working directory when the shell launched the application and doesn't include any working directory changes done by the app itself (which might be why the OP is seeing this behavior).

    So I thought I'd add some other ways that should give you the current working directory (not the application's binary location):

    // using where a relative filename will end up
    QFileInfo fi("temp");
    cout << fi.absolutePath() << endl;
    
    // explicitly using the relative name of the current working directory
    QDir dir(".");
    cout << dir.absolutePath() << endl;
    

提交回复
热议问题