Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QProcess and QProcessEnvironment not sending env to cmd.exe
QtWS25 Last Chance

QProcess and QProcessEnvironment not sending env to cmd.exe

Scheduled Pinned Locked Moved Unsolved General and Desktop
qprocessqprocessenviron
6 Posts 3 Posters 3.0k Views
  • 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.
  • TrilecT Offline
    TrilecT Offline
    Trilec
    wrote on last edited by
    #1

    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
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • TrilecT Offline
        TrilecT Offline
        Trilec
        wrote on last edited by
        #3

        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
        0
        • Chris KawaC Online
          Chris KawaC Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          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
          1
          • TrilecT Trilec

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

            D Offline
            D Offline
            Devopia53
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • TrilecT Offline
              TrilecT Offline
              Trilec
              wrote on last edited by
              #6

              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
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved