Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved QProcess and QProcessEnvironment not sending env to cmd.exe

    General and Desktop
    qprocess qprocessenviron
    3
    6
    2471
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Trilec
      Trilec last edited by

      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();
      }

      If ts not in the computer it doesn't exist!...

      D 1 Reply Last reply Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators last edited by

        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 to startDetached(). This is a long-standing shortcomming of QProcess. See QTBUG-2284.

        1 Reply Last reply Reply Quote 0
        • Trilec
          Trilec last edited by

          Thanks for the feedback,
          I suppose this then begs the question, how does one go about setting up an Environment that can be passed to a spin off process.?
          I want to give the user an ability to create further processes and not hang from the first process.

          If ts not in the computer it doesn't exist!...

          1 Reply Last reply Reply Quote 0
          • Chris Kawa
            Chris Kawa Moderators last edited by Chris Kawa

            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.
            1 Reply Last reply Reply Quote 1
            • D
              Devopia53 @Trilec last edited by

              This post is deleted!
              1 Reply Last reply Reply Quote 0
              • Trilec
                Trilec last edited by

                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?

                If ts not in the computer it doesn't exist!...

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post