how to set current working directory so that QFile gets relative paths right
-
How to set the current working directory (CWD) to where the application was started from, so that QFile finds paths relative to the CWD?
Following https://forum.qt.io/topic/88626, I changed my code into
QString name = argv[1]; QDir::setCurrent(qApp->applicationDirPath()); QFile file(name);
Let my application have the absolute path /home/A/app.
Let an input file have the absolute path /home/B/data.Now this works:
$ cd /home/A $ app ../B/data
But not this:
$ cd /home $ A/app B/data
It fails because the current path is set to where the application resides (/home/A), not to where it was started from (/home). This makes no sense, as it conflicts with Unix standards and habits. How to get it right?
-
From the documentation for applicationDirPath()
Returns the directory that contains the application executable.
So it sounds to me like its doing exactly what it's supposed to be doing.
I guess you still have to do: A/app ../B/data@mranger90, you are certainly correct about qApp->applicationDirPath().
I meant to ask what to do instead. I definitely need to get command line arguments interpreted in the standard way.
-
QDir::setCurrent(qApp->applicationDirPath());
requires you to callA/app ../B/data
as the argument is a path relative toA/app
if you want the path to be ralative to the current directory (
A/app B/data
) just don't change it and deleteQDir::setCurrent(qApp->applicationDirPath());
-
@Joachim-W I guess you don't need to use QDir:setCurrent at all, since per documentation:
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
so in your example
A/app B/data
your current directory is just the directory at which this application was started by the parent process i.e. /home and using just QFile file(name) (without calling setCurrent() previously) should get the desired file
-
How to set the current working directory (CWD) to where the application was started from, so that QFile finds paths relative to the CWD?
Following https://forum.qt.io/topic/88626, I changed my code into
QString name = argv[1]; QDir::setCurrent(qApp->applicationDirPath()); QFile file(name);
Let my application have the absolute path /home/A/app.
Let an input file have the absolute path /home/B/data.Now this works:
$ cd /home/A $ app ../B/data
But not this:
$ cd /home $ A/app B/data
It fails because the current path is set to where the application resides (/home/A), not to where it was started from (/home). This makes no sense, as it conflicts with Unix standards and habits. How to get it right?
@Joachim-W said in how to set current working directory so that QFile gets relative paths right:
How to set the current working directory (CWD) to where the application was started from
As others have said, the current working directory is where the application was started from!
-
sorry, @VRonin, @Pablo-J-Rogina, @JonB: your advice to simply delete the instruction
QDir::setCurrent(qApp->applicationDirPath());
may be consistent with docs, and it is certainly consistent with what I originally had expected from Qt ... but empirically it does not work. If I delete said instruction, then relative paths are interpreted relative to my home directory. Which may coincide with neither the directory where the app resides in, nor the directory where it was started from.
Last hope for a simple answer: maybe my app is misconfigured by some insane CMake instruction? Are there environment variables that would overwrite the default interpretation of relative paths?
-
sorry, @VRonin, @Pablo-J-Rogina, @JonB: your advice to simply delete the instruction
QDir::setCurrent(qApp->applicationDirPath());
may be consistent with docs, and it is certainly consistent with what I originally had expected from Qt ... but empirically it does not work. If I delete said instruction, then relative paths are interpreted relative to my home directory. Which may coincide with neither the directory where the app resides in, nor the directory where it was started from.
Last hope for a simple answer: maybe my app is misconfigured by some insane CMake instruction? Are there environment variables that would overwrite the default interpretation of relative paths?
@Joachim-W
Well it doesn't for me....
How are you starting/running your application? For example, are you invoking it from a desktop icon?
Print out what the current directory is on application start-up? -
@Joachim-W
Well it doesn't for me....
How are you starting/running your application? For example, are you invoking it from a desktop icon?
Print out what the current directory is on application start-up? -
@Joachim-W
And, presumably, you're saying that "command line" is in a terminal, with a non-home current directory?
What doesQDir::current()
orQDir::currentPath()
return? -
@Joachim-W with due respect, this is working for me (consistently with documentation) (Lubuntu, Qt 5.9, console application):
#include <QCoreApplication> #include <QDebug> #include <QDir> #include <QFile> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); qDebug() << QDir::currentPath(); QString name = argv[1]; QFile file(name); qDebug() << file.open(QIODevice::ReadOnly | QIODevice::Text); return a.exec(); }
with the following output:
pablo@lubuntu64:/home$ A/app B/data.txt "/home" true
just in case, the contents of corresponding folders A and B
pablo@lubuntu64:/home$ ls A app pablo@lubuntu64:/home$ ls B data.txt
-
deep in the code, in an inappropriate location and for unkown reasons, there was a statement
QDir::setCurrent(QDir::homePath());
which obviously caused the described behavior, confused me for days, and made me unjustly suspect Qt of doing insane things.
Thanks to you all for helping me in interpreting the Qt docs and in locating the bogous instruction.
-
The ultimate cause of my problem is a conflict between command-line interface and GUI:
For a file dialog in a GUI, it is perfectly reasonable to start from the user's home directory, whereas for a command-line interface it is insane to overwrite the current working directory.
-
The ultimate cause of my problem is a conflict between command-line interface and GUI:
For a file dialog in a GUI, it is perfectly reasonable to start from the user's home directory, whereas for a command-line interface it is insane to overwrite the current working directory.
@Joachim-W said in how to set current working directory so that QFile gets relative paths right:
For a file dialog in a GUI, it is perfectly reasonable to start from the user's home directory, whereas for a command-line interface it is insane to overwrite the current working directory.
QFileDialog
has setDirectory there is no need to mess up the working directory -
thanks to @JonB's questions, I located the problem: the current directory changes while my application is running, namely when the singleton instance of Mainwin is created.
@Joachim-W said in how to set current working directory so that QFile gets relative paths right:
thanks to @JonB's questions, I located the problem: the current directory changes while my application is running, namely when the singleton instance of Mainwin is created.
Yep, that's the only thing which makes sense!
I meant to ask what to do instead. I definitely need to get command line arguments interpreted in the standard way.
Now, doubtless you already realise this, but if you are dealing with relative paths received from the command line, you'd better deal with them (even if it's only to make them absolute) before you hit that code which changes the working directory (unless they really intentionally relative to the home directory in your app for some reason).
whereas for a command-line interface it is insane to overwrite the current working directory.
I see no reason why a file dialog for read/write should not be relative to the current directory, rather than say the home directory, but of course depends on context.
Finally, as @VRonin says, you can (and should) use
QFileDialog::setDirectory()
rather than actually changing directory if you only want that for the purpose of the file dialog.