[英]How to check if QProcess is executing correctly?
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;
I am running the above code. 我正在运行上面的代码。 When I check for an error condition after comparing not similar text files, isEmpty()
returns false
. 当我比较不相似的文本文件后检查错误情况时, isEmpty()
返回false
。
How do I check if the QProcess
execution occurred without errors? 如何检查QProcess
执行是否没有错误?
I use QProcess::error()
to query the last error (for quick debugging), however a "nicer" way to do it: 我使用QProcess::error()
查询最后一个错误(用于快速调试),但是使用“更精细”的方法来执行此操作:
// capture any errors (do this before you run the process)
connect(&process_sdcompare, &QProcess::errorOccurred, this, &ThisClass::processError);
Then define slot: 然后定义插槽:
ThisClass::processError(QProcess::ProcessError error)
{
qDebug() << "error enum val = " << error << endl;
}
update 更新
Or with Lambda: 或使用Lambda:
// No need to define a slot function...
connect(&process_sdcompare, &QProcess::errorOccurred, [=](QProcess::ProcessError error)
{
qDebug() << "error enum val = " << error << endl;
});
I think all the status and error functions are pretty useless to figure out if the process execution was actually successful. 我认为所有状态和错误功能都无法确定流程执行是否成功。 Example, trying to execute a program which does not exist (under windows): 例如,尝试执行一个不存在的程序(在Windows下):
error(): QProcess::ProcessError(FailedToStart)
exitStatus(): QProcess::ExitStatus(NormalExit)
exitCode(): 0
errorString(): "Process failed to start: Das System kann die angegebene Datei nicht finden."
If the process was actually executed successfully: 如果该过程实际上已成功执行:
error(): QProcess::ProcessError(UnknownError)
exitStatus(): QProcess::ExitStatus(NormalExit)
exitCode(): 0
errorString(): "Unknown error"
So I would consider "Unknown error" not an indicator for success. 因此,我认为“未知错误”不是成功的指标。
The solution I think could look like this: 我认为的解决方案可能是这样的:
QProcess *m_proc = new QProcess(this);
bool errorOccured = false;
QProcess::ProcessError procError;
QObject::connect(m_proc, &QProcess::errorOccurred, [&](QProcess::ProcessError error)
{
procError = error;
errorOccured = true;
});
m_proc->start(...);
When waitForFinished
returns, it returns a bool
that broadly indicates success. 当waitForFinished
返回时,它返回一个大致表示成功的bool
。 As Karsten recommended, you can then check for actual success or failure using QProcess::exitCode
. 按照Karsten的建议,然后可以使用QProcess::exitCode
检查实际的成功或失败。
This is how you use QProcess
这就是您使用QProcess
QProcess process_sdcompare;
QStringList args_sdcompare;
args_sdcompare << "Filename" << "Filename";
process_sdcompare.setProgram("diff");
process_sdcompare.setArguments(args_sdcompare);
process_sdcompare.start();
if (process_sdcompare.waitForStarted() && process_sdcompare.waitForFinished()) {
QString StdOut_sdcompare = process_sdcompare.readAllStandardOutput();
QString StdError_sdcompare = process_sdcompare.readAllStandardError();
if(StdOut_sdcompare.isEmpty())
return 1;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.