Get current working directory in a Qt application
Thank you RedX and Kaz for your answers. I don't get why by me it gives the path of the exe. I found an other way to do it :
QString pwd("");
char * PWD;
PWD = getenv ("PWD");
pwd.append(PWD);
cout << "Working directory : " << pwd << flush;
It is less elegant than a single line... but it works for me.
How to get current working directory path of a Qt application?
Your application cannot know where the project is, because this is a qt-creator option. If you want to run your binary in the project path you have to setup your working directory inside qt-creator to the correct path. Then QDir::currentPath()
will return the path you want to have.
goto: projects->[environment]->run->working directory
Path to the project current dir in qt
Based on your comment, you are trying to:
Access some images which are being used in my program. I could of course put them into build target directory, but it becomes uncomfortable to pass my code to others.
The approach to store resource files in the project source directory and rely on such structure at runtime is not a greatest idea. I can't imagine the situation when mixing the concepts of initially decoupled source and build directories could be useful, correct me if I'm wrong.
So, according to your needs:
- The most simple and plain way is to use the Qt resource system. The resource files are simply embedded into the executable, so there will be no need to access the file system.
- Another way is the automatic deployment of the needed files. This answer describes the way to copy your files to the target directory using
qmake
.
How to determine the directory of the executable file using Qt?
Use QCoreApplication::applicationDirPath()
instead of QDir::currentPath()
.
QCoreApplication::applicationDirPath()
returns a QString
with the path of the directory that contains the application executable, whereas QDir::currentPath()
returns a QString
with the absolute path of the current directory of the current running process.
This "current directory" is generally not where the executable file resides but where it was executed from. Initially, it is set to the current directory of the process which executed the application. The current directory can also be changed during the lifetime of the application process and is mostly used to resolve relative paths at run time.
So in your code:
QString cfg_name = QDir::currentPath() + "/config.cfg";
QFile File(cfg_name);
should open the same file as
QFile File("config.cfg");
But you probably just want
QFile File(QCoreApplication::applicationDirPath() + "/config.cfg");
How to change the current working directory?
copies it to the working directory of Qt
Not sure what exactly you mean by "Qt" in this context. If it is where the library is installed, you should associate that path with the file name then to be processed rather than setting the current working directory to be fair.
But why do you want to change the working directory at all? While you may want to solve one problem with it, you might instantly introduce a whole set of others. It feels like the XY problem. I think you will need a different solution in practice, like for instance the aforementioned.
If you still insist on changing the current working directory or whatever reason, you can use this static method:
bool QDir::setCurrent(const QString & path)
Sets the application's current working directory to path. Returns true if the directory was successfully changed; otherwise returns false.
Therefore, you would be issuing something like this:
main.cpp
#include <QDir>
#include <QDebug>
int main()
{
qDebug() << QDir::currentPath();
if (!QDir::setCurrent(QStringLiteral("/usr/lib")))
qDebug() << "Could not change the current working directory";
qDebug() << QDir::currentPath();
return 0;
}
main.pro
TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp
Build and Run
qmake && make && ./main
Output
"/tmp/stackoverflow/change-cwd"
"/usr/lib"
Qt how to open a file in current dir ? or what's wrong with this?
Try to use QCoreApplication::applicationDirPath() instead of QDir::currentPath().
For details see http://doc.qt.io/qt-5/qcoreapplication.html#applicationDirPath
Changing current path of a Qt Application
The problem is the path you are using is a full path to a config file containing the file name. When you try to change the directory to this path the command will fail because oldConfigFileName is a file not an existing folder. A simple way to fix this is to use QFileInfo to remove the filename part from the path then use that as the directory.
QFileInfo fi(oldConfigFileName);
bool res = QDir::setCurrent(fi.path());
if(res)
{
qDebug() << "Path Changed";
}
else
{
qDebug() << "Path not changed";
}
Getting current working drive in Qt
The QStorageInfo class provides information about a drive - to get one for a directory:
QStorageInfo info(dir);
qDebug() << "Name: " << info.name();
qDebug() << "Root path: " << info.rootPath();
Related Topics
Which MACro to Wrap MAC Os X Specific Code in C/C++
Opening a Window That Has No Title Bar with Win32
Qt 4.X: How to Implement Drag-And-Drop Onto the Desktop or into a Folder
Quick Sort at Compilation Time Using C++11 Variadic Templates
Load Shared Library by Path at Runtime
In C++ I Cannot Grasp Pointers and Classes
How to Convert a String of Hex Values to a String
Compile Program for 32Bit on 64Bit Linux Os Causes Fatal Error
Proper Way of Casting Pointer Types
Reference to Non-Static Member Function Must Be Called
Setting Width in C++ Output Stream
Intel Avx: 256-Bits Version of Dot Product for Double Precision Floating Point Variables
C++ on Small-Footprint Microcontrollers
Dropping Privileges in C++ on Windows
How to Asynchronously Copy Memory from the Host to the Device Using Thrust and Cuda Streams