【问题标题】:How to read when QProcess need user input with Qt当 QProcess 需要用户使用 Qt 输入时如何阅读
【发布时间】:2020-05-17 21:23:18
【问题描述】:

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

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

但是当我尝试为“make flash”做同样的事情时,会出现问题,因为控制台正在等待用户输入,他必须按下嵌入式系统上的按钮。

但是 QProcess 仅在脚本完成时返回标准输出,所以我没有“按下按钮”的消息。

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

我尝试了这里所说的: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 是一个用于在我的界面中显示控制台报告的类

[编辑] 我尝试了弗拉基米尔所说的。不幸的是我没有我的材料,所以我无法测试它,但我已经完成了这个脚本(test.bat)来测试:

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


    }

但它什么也没做

【问题讨论】:

  • 不要立即使用waitForFinished(),它似乎永远不会在你的情况下完成并阻止输出。只需启动进程并连接输出信号:stackoverflow.com/a/49059057/4149835 并且还可以显示控制台窗口:stackoverflow.com/a/52265222/4149835
  • 感谢您的回答。由于我现在无法测试(因为我没有材料),我已经完成了这个小脚本来测试:``` set /p answer=Do you want to create it now (Y/N) ? ``` 。不幸的是,它不起作用。我已经更新了我的问题,以便向您展示新版本。

标签: c++ qt qprocess


【解决方案1】:

我的test.bat

set /p answer=Do you want to create it now (Y/N)?
echo "user response:" %answer%
pause

代码启动批处理命令,读取它的输出并将答案写入进程:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多