Kill an application running outside a GUI with QProcess
-
Hello guys!
I have created a GUI where, one of your functions, is to run an application called TerraME with QProcess. The problem is that i have to put at the GUI an button for the user stop this application, but my GUI is unable to interact with the user when the TerraME is still running.
Here follows part of my code:
@
QProcess program;
QStringList parameters;parameters << this->workspace + "/coupled/coupled_model.lua"; program.start("TerraME", parameters); if(!program.waitForStarted()){ if(QMessageBox::Yes == QMessageBox::question(this, tr("TerraME is not configured"), tr("TerraME is not defined as a environment variable. Do you wanna select your executable?"), QMessageBox::Yes|QMessageBox::No)){ QString terraPath = configureTerraME(); program.start(terraPath, parameters); } else { QMessageBox::information(this, tr("Process canceled"), tr("Process canceled by user"));
// this->ui->textBrowser->clear();
return "";
}
}program.waitForFinished(-1); QString outputOk(program.readAllStandardOutput()); QString outputErr(program.readAllStandardError()); QString output = outputOk + outputErr; return output;@
This "return output" is just to show the errors and the application output at my GUI.
-
Because QProcess::waitForFinished() will block your program ;-)
Instead of calling that on, use Singals & Slots and connect your slot to the finished() signal of QProcess.
This way, you application stay responsive while the process is running and still react when it finishes.
Also, instead of readAllStandardOutput() once, better connect to the readyReadStandardOutput() signal...
-
[quote author="MuldeR" date="1366233632"]Because QProcess::waitForFinished() will block your program ;-)
Instead of calling that, use Singals & Slots and connect your slot to the finished() signal of QProcess.[/quote]
Does you have any example code for that?
-
It's straight forward, if you are familiar with the concept of Singals & Slots.
So you probably want to start here:
http://qt-project.org/doc/qt-4.8/signalsandslots.htmlAnd then see here:
http://qt-project.org/doc/qt-4.8/qprocess.html#signals--
More extensive example:
https://github.com/lordmulder/LoggingUtil/blob/master/src/LogProcessor.cpp