How to capture the output of the Qt program?
-
I have two Qt GUI programs, and I'm trying to use QProcess in one program to get the output of the other Qt program, like this:
QString prog = "myQtProgram.exe"; QProcess *subProcess = new QProcess; connect(subProcess, &QProcess::readyReadStandardOutput, subProcess, [=] { QString output = subProcess->readAllStandardOutput(); qDebug() << output; }); connect(subProcess, &QProcess::readyReadStandardError, subProcess, [=] { QString output = subProcess->readAllStandardError(); qDebug() << output; }); subProcess->start(prog);
But when this code runs, it cannot get any output from another Qt program.
I found that QtCreator can capture the output of the program even though I run the same program as QtCreator.
So how does QtCreator do it? Or, what should I do to capture the output of another Qt program?Thank you so much!
-
I have two Qt GUI programs, and I'm trying to use QProcess in one program to get the output of the other Qt program, like this:
QString prog = "myQtProgram.exe"; QProcess *subProcess = new QProcess; connect(subProcess, &QProcess::readyReadStandardOutput, subProcess, [=] { QString output = subProcess->readAllStandardOutput(); qDebug() << output; }); connect(subProcess, &QProcess::readyReadStandardError, subProcess, [=] { QString output = subProcess->readAllStandardError(); qDebug() << output; }); subProcess->start(prog);
But when this code runs, it cannot get any output from another Qt program.
I found that QtCreator can capture the output of the program even though I run the same program as QtCreator.
So how does QtCreator do it? Or, what should I do to capture the output of another Qt program?Thank you so much!
subProcess->setProcessChannelMode(QProcess::ForwardedChannels);
This should redirect the subprocess output to the callers output
https://doc.qt.io/qt-6/qprocess.html#setProcessChannelMode
https://doc.qt.io/qt-6/qprocess.html#ProcessChannelMode-enumConsider the warning after the enum docs. Could be that you have to use
QProcess::SeparateChannels
and get the output youself from the subprocess -
subProcess->setProcessChannelMode(QProcess::ForwardedChannels);
This should redirect the subprocess output to the callers output
https://doc.qt.io/qt-6/qprocess.html#setProcessChannelMode
https://doc.qt.io/qt-6/qprocess.html#ProcessChannelMode-enumConsider the warning after the enum docs. Could be that you have to use
QProcess::SeparateChannels
and get the output youself from the subprocess@Redman Thank you, I tried it your way, and inspired me to think and try more, but the results ended in failure.
In fact, I want to get all the output of the Qt program, for example, I deliberately raise an error: Cannot move to target thread (0xe33e8ff8c8), I don't know where this message comes from, but I want to get it.
I have tried to set WIN32_EXECUTABLE TRUE on Windows to display the console, but this causes the Windows console to read user input at some point, which causes the application to block. -
@Redman Thank you, I tried it your way, and inspired me to think and try more, but the results ended in failure.
In fact, I want to get all the output of the Qt program, for example, I deliberately raise an error: Cannot move to target thread (0xe33e8ff8c8), I don't know where this message comes from, but I want to get it.
I have tried to set WIN32_EXECUTABLE TRUE on Windows to display the console, but this causes the Windows console to read user input at some point, which causes the application to block.@HyEISN sounds like you may want to learn about the difference between stdout and stderr.
QProcess supports both, and you likely want to combine them into one stream.
some useful calls:
myProcess->setProcessChannelMode(QProcess::MergedChannels);
myProcess->start(QLatin1String("myExe"), arguments, QIODevice::ReadOnly);
myProcess->waitForFinished(30000);
-
I have two Qt GUI programs, and I'm trying to use QProcess in one program to get the output of the other Qt program, like this:
QString prog = "myQtProgram.exe"; QProcess *subProcess = new QProcess; connect(subProcess, &QProcess::readyReadStandardOutput, subProcess, [=] { QString output = subProcess->readAllStandardOutput(); qDebug() << output; }); connect(subProcess, &QProcess::readyReadStandardError, subProcess, [=] { QString output = subProcess->readAllStandardError(); qDebug() << output; }); subProcess->start(prog);
But when this code runs, it cannot get any output from another Qt program.
I found that QtCreator can capture the output of the program even though I run the same program as QtCreator.
So how does QtCreator do it? Or, what should I do to capture the output of another Qt program?Thank you so much!
@HyEISN
When you have a GUI program that does not normally produce any output, neither on stdout nor stderr. I am not sure what you are saying you see from where under what circumstances.In principle the code you showed is fine. If, say, you make the sub-process program a console application and have it just go like
printf("Hello world\n")
and exit you should find you receive that string back in your calling (UI) program. Does that work?