How to reset after call to QProcess::setStandardOutputFile so that outout is directed to stdout again

Viewed 66

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.

2 Answers

I do this :

  1. Create one shell script file .sh and add these scripts:

#!/bin/sh

echo test1

echo test2 > /home/parisa/process/my_file_path.txt

echo test3 

  1. then set chmod 777 script.sh and Run this program:

#include <QCoreApplication>
#include <QProcess>
#include <iostream>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QProcess* myProcess = new QProcess();
    myProcess->start("/home/parisa/process/script.sh");
    myProcess->waitForFinished();
    std::string str = myProcess->readAllStandardOutput().toStdString();

    std::cout <<str <<std::endl;

    delete myProcess;



    return a.exec();
}

The output:

enter image description here

UPDATED:

Another way that I try is that use finished. For Reset, you need to start your Process and then call finished and then start it again. Like this code :


#include <QCoreApplication>
#include <QProcess>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    QProcess* myProcess = new QProcess();

    myProcess->start("echo", QStringList() <<"test1");
    myProcess->waitForFinished();

    auto result = myProcess->readAll();
    qDebug()<<result;

    myProcess->finished(0);

    myProcess->setStandardOutputFile("/home/parisa/process/my_file_path.txt");
    myProcess->start("echo", QStringList() <<"test2");
    myProcess->waitForFinished();

    myProcess->finished(0);
    myProcess->setStandardOutputFile({});
    myProcess->start("echo", QStringList() <<"test3");
    myProcess->waitForFinished();

    auto result2 = myProcess->readAll();

    qDebug()<<result2;

    return app.exec();
}

The output:

enter image description here

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 obsolete start method. It does not even exist in Qt 6 anymore.

You should switch to the non-obsolete method where you pass the arguments in as a QStringList.

Related