Calling binaries using QProcess
-
Hi all.
I am trying to develop a gui using QT that runs OpenFoam (a c++ library) behind. tried to call a solver from that library using qprocess:
@
solverProcess = new QProcess(this);solverBin = "/OF_APPBIN/mysolver";
testDir = "/fullPathTestDir";
args << "-case" << testDir;solverProcess->start(solverBin, args);
if (solverProcess->waitForStarted()) // done@
This code does not produce any errors and ends successfully however runs nothing.
There is nothing wrong with the full path of the solver and test directory. I printed their values and could run manually on a terminal without any problems.
Any help is appreciated.
Note: Operating system is Ubuntu 10.10 and QT version 4.7
-
First, I would try connecting the signals from QProcess readyRead(), stdOutput(), and stdError() to see if there is dependencies missing (this could cause QProcess to silently fail to start).
Also, you'll need to define the path to the shell you are using at your program, then your first argument should be the binary location.
For example, you use sh as your shell.
@
solverProcess = new QProcess(this);
solverProcess->start("/bin/sh", QStringList() << "/OF_APPBIN/mysolver" << "-case");
@If you use bash, the just replace sh with bash.
-
Well thanks for the idea.
I changed the code a little, still doesn't work though.
This is now what I am trying:
@
m_SolverProcess->setStandardOutputFile("~/myTestDir/test.out", QIODevice::Append);m_SolverProcess->start("/bin/sh", QStringList() << "~/myTestDir/test.sh");
if (m_SolverProcess->waitForStarted()) {
if (m_SolverProcess->state() == QProcess::Running) {
QMessageBox::information(NULL, "runSolver", "Done");
}
}
@And the content of the test shell script includes the line that I actually want to run:
@
#!/bin/bashecho "Starting Run"
echo "-------------"
echosimpleFoam > log.o
@When I run the code above the qt message box appears and the log files are created, as expected test.out has "starting run" and log.o is empty.
I can run this shell script successfully on a terminal manually, so I am sure that simpleFoam is a binary that runs well. It's permissions are also correctly assigned.
I can't seem to understand where the problem would be. Any ideas are very welcome.
-
you can try to call the program in its directory:
m_SolverProcess->setWorkingDirectory(prog.path());
m_SolverProcess->start(qa("./")+prog.fileName(),args); -
As I stated before, you can check the ouput of the QProcess to standard out and standard error...
If you want to run a direct binary, then use this to test:
@
QProcess *process = new QProcess(this);
process->start("/bin/sh", QStringList() << "filename" << "argument");
if (!process->waitForStarted(100)){ // 100mS timeout wait
qDebug() << "STDOUT: " << process->readAllStandardOutput();
qDebug() << "STDERR: " << process->readAllStandardError();
}
@If you want to run a script, the shell is already included, therefore use this to test:
@
QProcess *process = new QProcess(this);
process->start("/path/to/script/script.sh");
if (!process->waitForStarted(100)) // 100mS timeout wait
qDebug() << "STDOUT: " << process->readAllStandardOutput();
qDebug() << "STDERR: " << process->readAllStandardError();
}
@Does qDebug print anything out to the console? These can all be used event driven as well using the readyRead() signals...make sure you include <QDebug>
-
I moved the installation of the third party library from my home directory to /opt and issue is solved.
But thanks for the tip:
@qDebug() << "STDERR: " << process->readAllStandardError();@
that is very useful.