Change PATH environment variable for cmd.exe with QProcessEnvironment

前端 未结 2 1264
小蘑菇
小蘑菇 2021-01-26 12:15

I want to start cmd.exe from a Qt app with a specific PATH set. I insert \"Path\" in QProcessEnvironment and set that environment to QProcess. Then I startDetached \"cmd\". On t

2条回答
  •  灰色年华
    2021-01-26 13:04

    The startDetached method is a static method. So all the state that you applied to the process object is just ignored, because the method cannot see it. If you start the process with start() instead, the new process will pick up your environment.

    process.start("cmd", args);
    

    Of course, you want to the new process to be detached so that the parent can terminate without forcing the new process also to terminate. From what I can tell, the QProcess class does not offer you a way to achieve that easily. You could modify the environment of the parent process so that the new process inherits those modifications, but that does not sound at all desirable.

    This question presents a possible workaround: Detaching a started process.

提交回复
热议问题