Search code examples
c++qt4environment-variables

How can I set PATH variable using QT?


How can I get and set PATH variable using QT 4.8? I know I can get PATH variable values using getenv from the STL but don't know how to set it using STL or any Qt based method?

If QT has a function for it, I would like to know and use it rather than going and using Windows API for it.


Solution

  • You can use setenv from stdlib.h to set PATH to a new value.

    setenv("PATH","/new/path/value",1)
    

    However, this is a non-standard extension to the standard headers, and will only affect sub-processes spawned by the calling process. In order to change environment variables for all new processes spawned, a system-specific method must be used. For windows, the PATH variable can be changed in the

    HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment
    

    registry key. This will make sure the PATH is set for all new processes, and will apply over a reboot.