read QProcess output to string
Did you try QByteArray QProcess::readAllStandardOutput()
docs - here
QString
can be instantiated from QByteArray
:
QString output(pingProcess.readAllStandardOutput());
As others mentioned, and I join to them, you should not use execute
method and replace it with:
pingProcess.start(exec, params);
pingProcess.waitForFinished(); // sets current thread to sleep and waits for pingProcess end
QString output(pingProcess.readAllStandardOutput());
Using QProcess to read standard output
As I said in my comments one of the main problems is that when you run tutorial3 that process is separated so you can not get the output. Therefore, I recommend executing it directly, and QProcess is probably a local variable, eliminating after printing an empty text, a possible solution is to create a pointer. Another improvement would be to use the readyReadStandardOutput and readyReadStandardError signals since the impressions are not automatic.
QProcess *process = new QProcess(this);
connect(process, &QProcess::readyReadStandardOutput, [process, this](){
QString output =process->readAllStandardOutput();
qDebug() << "output: "<< output;
});
connect(process, &QProcess::readyReadStandardError, [process](){
QString err = process->readAllStandardError();
qDebug() << "error: "<<err;
});
process->setWorkingDirectory("/home/pi/Desktop/tutorial3App/bin/")
process->start("tutorial3", QStringList() << "start");
How to get STDOUT from a QProcess?
Before starting your process call:
process.setProcessChannelMode(QProcess::MergedChannels);
It will cause printing everything (even STDERR output) to STDOUT output.
How to read when QProcess need user input with Qt
My test.bat
set /p answer=Do you want to create it now (Y/N)?
echo "user response:" %answer%
pause
The code starts the batch command, reads it output and writes the answer to the process:
QProcess *compilator = new QProcess(this);
connect(compilator, &QProcess::readyReadStandardOutput, [compilator, this]()
{
QString output = compilator->readAllStandardOutput().simplified();
qDebug().noquote() << "output: " << output;
if (output.contains("(Y/N)?", Qt::CaseInsensitive))
{
compilator->write("Y\n"); // write our answer to the process
}
else if (output.contains(". . .", Qt::CaseInsensitive))
{
compilator->write("\n"); // simulate press any key to process
}
});
connect(compilator, &QProcess::readyReadStandardError, [compilator, this]()
{
QString err = compilator->readAllStandardError().simplified();
qWarning().noquote() << "error: " << err;
});
// Handle the finished() signal:
connect(compilator, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
[compilator, this](int exitCode, QProcess::ExitStatus exitStatus)
{
qDebug() << "compilator process finished with exit code =" << exitCode
<< "and exit status =" << exitStatus;
});
compilator->start("test.bat");
if (compilator->waitForStarted()) // use to check start errors
{
qDebug() << "compilator process started ok";
}
else
{
qCritical() << "compilator process start FAILED:" << compilator->errorString();
}
Related Topics
Fseek Does Not Work When File Is Opened in "A" (Append) Mode
In C++, Is There a Difference Between "Throw" and "Throw Ex"
Why Do You Need to Append an L or F After a Value Assigned to a C++ Constant
Adding Header and .Cpp Files in a Project Built with Cmake
Partial Template Specialization Based on "Signed-Ness" of Integer Type
C++: How to Catch Mouse Clicks Wherever They Happen
Why Are Override and Final Identifiers with Special Meaning Instead of Reserved Keywords
How to Force My Application to Open One Exe Only? Qt, Linux
How to Turn on Multi-Cpu/Core C++ Compiles in the Visual Studio Ide (2008)
How to Reproduce Tcp Protocol 3-Way Handshake with Raw Sockets Correctly
Can This MACro Be Converted to a Function
Why Derive from a Concrete Class Is a Poor Design
How to Implement a Video Widget in Qt That Builds Upon Gstreamer
Scheduling Task in Using C++ on Linux and Windows Machine
Qt - Qpushbutton Text Formatting