Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if QProcess is executing correctly?

Tags:

c++

linux

qt

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.

How do I check if the QProcess execution occurred without errors?

like image 651
karthik poojary Avatar asked Oct 18 '25 03:10

karthik poojary


1 Answers

I use QProcess::error() to query the last error (for quick debugging), however a "nicer" way to do it:

// 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:

// No need to define a slot function...
connect(&process_sdcompare, &QProcess::errorOccurred, [=](QProcess::ProcessError error) 
{ 
    qDebug() << "error enum val = " << error << endl; 
});
like image 191
code_fodder Avatar answered Oct 20 '25 16:10

code_fodder