Search code examples
c++qtqprocessconsole-output

Reading console output of a detached process


Hi I am firing a detached process from Qt using QProcess. I want to read the console output of the process in a QString. Here is the code

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

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QProcess proc;
    proc.startDetached("C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", 
                        QStringList() << "/c" << "c:\\Qt\\Qt5.3.0\\Tools\\QtCreator\\bin\\tryScript\\firstBatch.bat");


    proc.waitForFinished();
    qDebug() << proc.readAllStandardOutput();

    return a.exec();
}

Solution

  • QProcess::startDetached is not a member function, its a static function, so

    proc.startDetached(...)
    

    is equivalent to :

    QProcess::startDetached(...)
    

    Hence there's no state or output in your proc variable for the detached process. Use the start() method if you want to start the process as a subprocess of your application and read its output.