Important: Please read the Qt Code of Conduct -

QProcess::setWorkingDirectory has no effect on Windows

  • Here's what I'm trying to do:

    @QProcess * process = new QProcess;

    process->start("qmake", QStringList() << "-tp" << "vc" << "-r");

    The directory "C:/temp_dir/" exists. I can clearly see that setWorkingDirectory has no effect, because this dir contains a valid qmake project, and Visual Studio project is not generated when I run the command. Also, 1.txt file is created in the .exe working dir and not in temp_dir, but I'm not sure if that's also a bug or expected behavior.

  • Moderators

    You use relative path in call to setStandardOutputFile. This path is relative to the working dir of your app, not the process you're about to start. So either enter path relative to your exe(not the working dir as it may vary!) or use a full path. That's the expected behavior.

    As for the setWorkingDirectory I think you came to wrong conclusions because it does work. You can easily check that by running a program that prints working directory.

    Does it work if you run it from command line? Are all environment variables etc. available to the process you start?

  • I've switched from QProcess::start to startDetached, and now I'm positive it doesn't work. The following command

    @QProcess::startDetached("cmd.exe", QStringList() << "/C cmd", "C:/test_folder/");@

    launches cmd.exe in the application's working dir, not in C:/test_folder. test_folder exists and is accessible.

  • Moderators

    It works for me. What Qt and Windows version are you using?

  • Qt 5.2.1, Windows 7 x64.
    Update: it turned out to be a very silly mistake on my part. I had 2 similar lines of code nearby, and of course the one I was editing wasn't the one that was being executed. It works now.

  • And yet I'm having problems. Working dir is now fine, but something in the way I use QProcess is wrong.

    I'm trying to start a console and execute arbitrary commands there. Like so:

    @QString command = "qmake -v > 1";
    QProcess::startDetached("cmd.exe", QStringList() << (QString("/C ") + command), workingDir);@

    And it works fine, file 1 is created and contains qmake -v output. However, this command:

    @QString command = "qmake -tp vc -r > 1";@
    Doesn't work. I launch it with workingDir set to a dir with a valid Qt project. File 1 is created, but it's blank. Qmake launches, but then closes down immediately, the output file is blank, no project is generated.

    "dir > 1" is executed as expected, however, printing the contents of workingDir and redirecting it into a file created also in workingDir. Any tip on how to debug this? I'd think it's a qmake issue, but if I run cmd from my workingDir and execute "qmake -tp vc -r" or "qmake -tp vc -r > 1" - it works!

Log in to reply