-1

I have a exe folder located in one folder, and then all of my config files in another. In order to run the program:

  • I need to direct the terminal to the config file folder
  • Then run the exe folder, with arguments.

Shown below, I have this working correctly using system calls, but I need a system agnostic way of doing this.

void MainWindow::on_pushButton_clicked()
{
    std::system("cd Path_To_ConfigFolder & Absolute_Path_To_Exe RunFile.txt > logfile.txt");
}

The above works great. Although it only works on Windows.. so I did some research on using QProcess to create a more system agnostic way.. and no matter what I try I can't seem to get this to work. See below:

void MainWindow::on_pushButton_clicked()
{
    QProcess p;
    QStringList params;
    p.setWorkingDirectory("Path_To_ConfigFolder");
    params << " RunFile.txt > logfile.txt";
    p.start("Absolute_Path_To_Exe", params);
    p.waitForFinished(-1);
}

Note: For the above example (using QProcess), I am \\ for all my paths, and all my paths are correct.

What am I doing wrong here?

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • 1
    You need to check all possible errors of course. See https://stackoverflow.com/questions/60969455/how-to-run-an-external-application-from-qt-program/60973204#60973204 – Vladimir Bershov Sep 25 '20 at 15:21

1 Answers1

2

Name of executable would be starting with ".\" if it is in local folder to be system-agnostic. Linux shell and PowerShell require that.

">" - output redirection isn't an argument of process, that won't work. You have to redirect output channel to your or a secondary process.

p.setStandardOutputFile("log_file.txt")
Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
  • Thanks for your help @Swift . How do I direct to the output file inside of p.start()? – Tyler Beauregard Sep 25 '20 at 15:40
  • 1
    @TBCoder92 Have a look at [`QProcess::setStandardOutputFile`](https://doc.qt.io/qt-5/qprocess.html#setStandardOutputFile). – G.M. Sep 25 '20 at 15:47
  • Placed `p.setStandardOutputFile(AbsPathToLogFile.txt)` above `p.start()` and removed space before `RunFile.txt` . Doing these all worked, thanks for the help! – Tyler Beauregard Sep 25 '20 at 16:09