QProcess and QProcessEnvironment not sending env to cmd.exe
-
Hi all,
New to QProcess, perhaps a setting Ive missed or windows 7 cmd.exe shell is odd.The following exert does not show the adjustments made to the QProcessEnvironment .
ie: once the shell comes up typing "set" does not show the newly inserted environment variables.This is a smaller test as the bigger one was also showing the same issue with a different app.
Any light on this issue much appreciated
int main(int argc, char *argv[])
{QApplication app(argc, argv); app.setApplicationName(app.translate("main", "Launcher")); app.setWindowIcon(QIcon(":/icon.png")); MainWindow w; w.show(); QProcess process; QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); env.insert("TMPDIR", "C:\\MyApp\\temp"); // Add an environment variable env.insert("MYTEST", "C:\\MyApp\\temp"); // Add an environment variable env.insert("PATH", "C:\\Bin"); process.setProcessEnvironment(env); process.startDetached("cmd.exe");
return app.exec();
} -
Hi, welcome to devnet.
You are setting environment for an instance of QProcess.
startDetached()
is a static method so it doesn't care about that.
Unfortunately there is no way to pass QProcessEnvironment tostartDetached()
. This is a long-standing shortcomming of QProcess. See QTBUG-2284. -
-
It can't be done nicely with current Qt API.
You have basically 2 options:
- Start the process normally (not detached) and hold on to the QProcess instance. You don't need to block but you need to keep the instance around. Also the process will be terminated when QProcess instance is destroyed (e.g. your app closes).
- Don't use Qt function. You can copy the implementation of
startDetached()
and inject your environment there.
-
Thanks again,
Does seem implementing the startDetached() is the better option ( if i understand correctly) as when the app closes , closing down the started sup processes could be problematic.On a side note:
Needing to keep the instance around is also an interesting complexity, if logic serves,- The app starts a process (perhaps stored in a QList Or QHash)
- Maintain the state so when the sup-process terminates i can ether re-use the process or remove the process and create another instance .
*with the understanding that if the main app closes all sup-process's will close.
Is the logic sound?