Here's a snippet of code to explain what I am trying to do
QProcess* myProcess = new QProcess();
myProcess->start("echo test1");
myProcess->waitForFinished();
myProcess->setStandardOutputFile("my_file_path");
myProcess->start("echo test2");
myProcess->waitForFinished();
myProcess->start("echo test3");
Here I want "test1" and "test3" to go to stdout and "test2" to file. However "test3" is also echoed to file. I would to reset the QProcess to redirect out to stdout again after "test2"
How do I do this?
PS: This snippet is not the actual code. It just demonstrates the issue I am facing.
CodePudding user response:
The solution is simply setting the standard output file to an empty string by ""
, QString()
, {}
, etc. It would be helpful if the documentation was explaining that hidden gem. I will submit a merge request later to update the documentation.
This code demonstrates the intended behaviour:
#include <QCoreApplication>
#include <QProcess>
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QProcess* myProcess = new QProcess();
myProcess->start("echo", {"test1"});
myProcess->waitForFinished();
myProcess->setStandardOutputFile("my_file_path");
myProcess->start("echo", {"test2"});
myProcess->waitForFinished();
myProcess->setStandardOutputFile({});
myProcess->start("echo", {"test3"});
return app.exec();
}
Please note that you should not use the
UPDATED: