当QProcess需要用户输入Qt时如何阅读 [英] How to read when QProcess need user input with Qt

查看:59
本文介绍了当QProcess需要用户输入Qt时如何阅读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Qt来实现允许为嵌入式系统开发的接口.

I'm using Qt in order to implement an interface allowing to develop for embedded system.

我遇到了一个问题:为了将程序闪存到嵌入式系统中,我使用QProcess,以便使用命令"make"和"make flash".为了没有任何问题,程序可以成功编译.

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.

但是当我尝试对"make flash"执行相同的操作时,由于控制台正在等待用户输入而出现问题,他必须按下嵌入式系统上的按钮.

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.

但是QProcess仅在脚本完成时才返回标准输出,因此我没有消息按下按钮".

But QProcess returns standard output only when the script is finished so I don't have the message "press a button".

所以我的问题是:我怎么知道QProcess何时需要用户输入?还是如果不可能,如何使用Qt动态打开控制台并启动命令?

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?

我试图在这里说些什么: https://www.qtcentre.org/threads/47538-QProcess-read-from-stdout-lively

I tried to what is said here: https://www.qtcentre.org/threads/47538-QProcess-read-from-stdout-lively

这是我的代码:

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");

其中m_logFrom是用于在我的界面中显示控制台报告的类

Where m_logFrom is a class used to display console report in my interface

我尝试了弗拉基米尔所说的话.不幸的是,我没有我的资料,所以我无法对其进行测试,但是我已经完成了此脚本(test.bat)以便进行测试:

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)?

这是我的新代码:

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");


    }

但是它什么都不做

推荐答案

我的 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();
}

这篇关于当QProcess需要用户输入Qt时如何阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆