Important: Please read the Qt Code of Conduct -

QT installer framework to set user PATH environment variable

  • I'd like to set user PATH environment variable for my Qt application and in my installscript.qs I do this:

    Component.prototype.createOperations = function()
    // call default implementation
    // ... add custom operations
    var winpath = installer.environmentVariable("PATH") + ";" + installer.value("TargetDir");

    However this will add system PATH environment variable to user PATH environment variable, and it will keep repeat everything if I run the installer again. That messes up the variable. What can I do? What is the right way of just adding my variable to the user PATH environment variable (and maybe remove it upon uninstallation)?

    Thanks a lot.

  • Lifetime Qt Champion


    Out of curiosity, why do you want to modify the user PATH environment variable ?

  • My application needs external dll (lets call it a.dll), and I think that a.dll calls other dll relative to its location. I tested it that I can't just move a.dll to same directory of my exe, but if I set the environment variable everything will work fine...

  • Lifetime Qt Champion


    tested it that I can't just move a.dll to same directory of my exe, but if I set the environment variable everything will work fine...

    That sounds strange. On Windows, both should work the same.

    I'd very upset if your app would modify my PATH. There are already enough strange entries that causes problems for other apps, e.g.

  • I think the a.dll is expecting some fix relative directory structure of dll (and other things). I think Windows program add environment variable all the time? Or can I create my own variable?

  • Lifetime Qt Champion

    Then you should check what exactly is expected for these .dlls.

    As @aha_1980, there are indeed lots of application that modify the PATH environment variable for usually no really good reasons and that can be mighty annoying.

  • Lifetime Qt Champion

    @Pauly I just hunted a bug where an app didn't work.

    It turned out, the app was using the wrong Qt DLLs from QtCreator, because that one was added to PATH

    So just think, fixing your case might break others.

  • Understood... So is there a way to specify relative path to a particular dll in Qt project, just as if that is added to the environment variable? That may solve my need without messing with the environment variable.

  • I have same problem, but on uninstall process, also, i get errors with "Error during uninstallation: " with empty description.

    I think, it happens because "addElevatedOperation" must have both DO and UNDO steps(but i dont know how to add UNDO step for uninstall)
    DO on installation step, UNDO on uninstall. Does anyone know how to solve this problem?

    Component.prototype.createOperations = function()
        component.addElevatedOperation("EnvironmentVariable","GST_PLUGIN_PATH", gstPluginPath, persistently, false);
        component.addElevatedOperation("EnvironmentVariable","GST_PLUGIN_SYSTEM_PATH", gstPluginSystemPath, persistently, false);
        component.addElevatedOperation("EnvironmentVariable","GSTREAMER_1_0_ROOT_X86", gstreamerRootPath, persistently, false);

  • @Pauly
    Does this work?

    qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));

    Obviously put in your own values for the path variable "QT_IM_MODULE" and its set to value "qtvirtualkeyboard".

  • Try this

    var winpath = installer.environmentVariable("PATH");
    component.addOperation( "Execute", "setx", ["PATH", "\"" + @TargetDir@ + winpath + "\""] ); 

    to add to user PATH or

    component.addOperation( "Execute", "setx", ["/M", "PATH",  "\"" + @TargetDir@ + winpath + "\""] ); 

    to add to system PATH (admin rights might be provided)

    Be noted that setx command truncate PATH to 1024 symbols. That mean, it might break the system in case the PATH variable was longer.

Log in to reply