问题
I'm using Qt in order to implement an interface allowing to develop for embedded system.
I'm facing a problem: In order to flash program into the embeded system I use QProcess, in order to use the command "make" and "make flash". To make there isn't any problem and the program compiles successfully.
But when I try to do the same thing for "make flash" there is a problem because the console is waiting for user input, he has to press a button on the embedded system.
But QProcess returns standard output only when the script is finished so I don't have the message "press a button".
So my question is: How can I know when QProcess needs user input? Or if it's impossible, how can I open a console dynamically with Qt and start a command?
I tried to what is said here: https://www.qtcentre.org/threads/47538-QProcess-read-from-stdout-lively
And here is my code :
compilator->start("make flash");
compilator->waitForFinished();
QByteArray errors = compilator->readAllStandardError();
QString stringError(errors);
QByteArray message = compilator->readAll();
QString stringMessage(message);
m_logForm->setText("Project path : "
+ pathProject + "\n"
+ "Flash finished with exit code " + QString::number(compilator->exitCode()) + "\n"
+ stringError + "\n"
+ stringMessage + "\n");
Where m_logFrom is a class used to display console report in my interface
[EDIT] I tried what Vladimir said. Unfortunatly I don't have my material so I can't test it but I've done this script (test.bat) in order to test :
set /p answer=Do you want to create it now (Y/N)?
Here is my new code :
QProcess *compilator = new QProcess(this);
connect(compilator, &QProcess::readyReadStandardOutput, [compilator, this](){
QString output =compilator->readAll();
qDebug() << "output: "<< output;
m_logForm->setText("Flash finished with exit code " + QString::number(compilator->exitCode()) + "\n"
+ output + "\n");
});
connect(compilator, &QProcess::readyReadStandardError, [compilator, this](){
QString err = compilator->readAllStandardError();
qDebug() << "error: "<<err;
m_logForm->setText("Flash finished with exit code " + QString::number(compilator->exitCode()) + "\n"
+ err + "\n");
});
m_logForm->setText("Flashing to serial port "+m_Serial + "\n");
compilator->setWorkingDirectory(pathProject);
compilator->start("test.bat");
}
But it do nothing
回答1:
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();
}
来源:https://stackoverflow.com/questions/60016900/how-to-read-when-qprocess-need-user-input-with-qt