0

In some places it suggests, I can't run a pre-built c++ binary with QProcess. at the same time there are other questions where people are executing shell scripts etc with QProcess so I am confused. Can I execute a pre-built c++ binary using QProcess. This binary reads a text file and creates two text files in return. I created a basic UI with GUI and have a button which when clicked calls the external binary. Running this with execute gives me an error of QIODevice: read: device not open. When I use start, no errors are reported. But no output files are created either. Any ideas whether this is permitted in qt or some other approach needs to be followed.

void MainWindow::on_startButton_clicked()
{
QString program = "./home/naveen/sdj";
QProcess *myProcess = new QProcess(this);
myProcess->start(program);
myProcess->waitForFinished();
qDebug() << myProcess->exitStatus();
qDebug() << myProcess->readAllStandardError();
}        

1 Answer 1

4

First, QProcess::execute() is a static method -- there's no reason to create a QProcess instance to use it. If you use QProcess::start(), it will execute the process asynchronous. You have to listen for a finished signal before you can check for a return code.

Second, are you sure this is what you intended?

QString program = "./home/naveen/sdj";

In *nix file systems, ./ means start in the current directory. So QProcess won't look for /home/naveen/sdj, it will instead look for /yourProjectBuildPath/home/naveen/sdj. I'm guessing that's not what you want.

Sign up to request clarification or add additional context in comments.

4 Comments

Sure that’s not the execution path, not the build path?
@Smar True, I'm assuming the most common case where the app is run by QtCreator.
@MrEricSir, I changed it to /home/naveen/sdj. still no execution though. Also, how do I wait for finished. can you please give a hint
Add a slot to your class, and use connect(myProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onProcessFinished(int, QProcess::ExitStatus))); The method should get called with whatever exit status the process had. This only applies if you use the start method, though. If you use the execute method, it is supposed to wait for the process to finish, [according to the doc](qt-project.org/doc/qt-4.7/qprocess.html). But as @MrErirSir said, you should use QProcess::execute(program) instead of declaring a new object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.