gpt4 book ai didi

c++ - 如何检查 QProcess 是否正确执行?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:22:34 26 4
gpt4 key购买 nike

QProcess process_sdcompare;
QString command_sdcompare;
QStringList args_sdcompare;

command_sdcompare = "diff";
args_sdcompare << "Filename" << "Filename";

process_sdcompare.start(command_sdcompare,args_sdcompare,QIODevice::ReadOnly);
process_sdcompare.waitForFinished();
QString StdOut_sdcompare = process_sdcompare.readAllStandardOutput(); //Reads standard output
QString StdError_sdcompare = process_sdcompare.readAllStandardError(); //Reads standard error

if(StdOut_sdcompare.isEmpty()) //the process output is checked here if empty it is a success
return 1;

我正在运行上面的代码。当我在比较不相似的文本文件后检查错误情况时,isEmpty() 返回 false

如何检查 QProcess 执行是否没有错误?

最佳答案

我使用 QProcess::error() 来查询最后一个错误(用于快速调试),但是这是一种“更好”的方法:

// capture any errors (do this before you run the process)
connect(&process_sdcompare, &QProcess::errorOccurred, this, &ThisClass::processError);

然后定义插槽:

ThisClass::processError(QProcess::ProcessError error)
{
qDebug() << "error enum val = " << error << endl;
}

更新

或者使用 Lambda:

// No need to define a slot function...
connect(&process_sdcompare, &QProcess::errorOccurred, [=](QProcess::ProcessError error)
{
qDebug() << "error enum val = " << error << endl;
});

关于c++ - 如何检查 QProcess 是否正确执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37961852/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com