QProcess fails to execute external executable

jj01 picture jj01 · Aug 6, 2013 · Viewed 7.5k times · Source

I am struggling to find a solution to my problem, but I simply have no clue how to solve it.

I am creating an user-interface for some programs I made (so you can through simply pressing a button start an executable). So I thought of using qt.

So I read a lot about the QProcess and tried to use it.

At the first executable of mine I tried to start it through QProcess::start(), but it didn't work so I tried it with QProcess:execute():

QProcess *proc = new QProcess(this);
QDir::setCurrent("C:\\DIRTOTHEEXE\\");
QString program="HELLO.exe";
proc->execute(program);

This executes my program perfectly and works nice.

So I tried to do the same with my other exe, but it didn't work

QProcess *myproc = new QProcess(this);
QDir::setCurrent("C:\\DIRTOTHEEXE\\");
QString program="HelloWorld.exe";
myproc->start(program);

The called executable simply prints "Hello World" and returns 0 then.

So now my question is: What could cause this behaviour and why can't I use QProcess::start() for the first executable?

Btw: I also tried to set the workingDirectory() to the path of the exe, but also that didn't work.

Hope someone can help me.

EDIT: So the program is executed but crashes right after printing out one line.

EDIT: Here the HelloWorld source.

#include <iostream>
using namespace std;

int main(int argc, char* argv[]) {
    cout<<"HELLO WORLD!!"<<endl;

    return 0;
}

Answer

TheDarkKnight picture TheDarkKnight · Aug 6, 2013

QProcess has 3 functions for starting external processes, such as: -

  • start
  • execute
  • startDetached

The latter two, execute and startDetached are static, so don't need an instance of QProcess to call them.

If you use start, you should at least be calling waitForStarted() to let the process setup properly. The execute() function will wait for the process to finish, so calling waitForStarted is not required.

As you've only posted a small amount of code, we can't see exactly what you're trying to do afterwards. Is that code in a function that ends, or are you trying to retrieve the output of the process? If so, you definitely should be calling waitForStarted if using start().

If you only want to run the process without waiting for it to finish and your program is not bothered about interacting with the process, then use startDetached: -

QProcess::startDetached("C:\\DIRTOTHEEXE\\HELLO.exe");