How to read when QProcess need user input with Qt

情到浓时终转凉″ 提交于 2020-11-29 21:20:58

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!