View Full Version : Difference between QProcess and exec()

18th November 2013, 19:41

I need to call a shell script from within my Qt 4.7 (on Linux) app and QProcess is giving me trouble.
Here is the code that does not work - meaning it does execute the shell script but the environment is false:

QProcess myProcess(this);
myProcess->start(prog, args);

I tried setting "PATH" and some other env-variables via myProcess->setEnvironment() but with no effect. But this works fine:

pid_t pID = fork();
if (pID == 0)
execl(prog,prog,args, (char *) 0);

Any suggestion on what is going on?


18th November 2013, 19:59
Can you give an example of how you are setting the environment (what are you passing to QProcess::setEnvironment())?

What happens if you call:

Also, the QProcess::setEnvironment function was deprecated in Qt 4.6 in favor of the QProcess::setProcessEnvironment(const QProcessEnvironment & environment). It should still work, but the trolls claim the latter is more efficient.

18th November 2013, 20:29
I tried all variations of setting the environment but I does not make any difference.

18th November 2013, 20:58
Have you tried this:

QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("PATH", env.value("PATH") + ":" + settings->scriptsDirectory); // change this
scriptProcess->start(command, args);

18th November 2013, 23:32
I have a 1st clue, as soon as I start re-directing stdout my script fails. Code:

int my_pipe[2];
if(pipe(my_pipe) == -1)
fprintf(stderr, "Error creating pipe\n");

pid_t pID = fork();
if (pID == 0) // child
close(my_pipe[0]); // child doesn't read
dup2(my_pipe[1], 1); // redirect stdout
execl(prog,prog,args, (char *) 0);
I think the problem might most likely be in the script than in Qt