简体   繁体   中英

If setProcessEnvironment the QProcess will not run

I have a simple piece of code that runs an external script (see below). The code runs great. Now I want to set an environment variable before running the script. When I do, the waitForStarted method fails with error code 0 and errorString " Exec format error ".

I've simplified my code, and can reproduce the error simply by calling setProcessEnvironment. On the code below, if I comment out the third line it runs great. if I uncomment the third line I get the error noted above. Can someone explain why simply setting the environment back to what it was would cause this error? (Running on Linux in case it matters, with Qt 5.1.0).

QProcess command;
QProcessEnvironment oldenvironment = QProcessEnvironment::systemEnvironment();
// If I comment out next line it runs fine!
command.setProcessEnvironment(oldenvironment);

command.start(commandpath, parameters);
if (command.waitForStarted(START_WAIT_TIME)) {
   qDebug() << "Started ok";
} else {
    qDebug() << "*** start error code: " << command.error();
    qDebug() << "*** start error string: " << command.errorString();
}

As well, commandpath is set to "/tmp/myscript.sh" and parameters is an empty QStringList

I've found what seems to be an iteresting bug in Qt. The program I'm launching is a bash script, and I did NOT have the #!/bin/bash line at the top of the file.

For some reason, without setting the environment Qt had no problem running the file. But once I set the environment, it would only run if I had the #! line at the top of the file. Somehow setting the environment changed how Qt exec'ed the file....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM