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. [SOLVED] Problem with cmd process
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Problem with cmd process

Scheduled Pinned Locked Moved Solved General and Desktop
32 Posts 5 Posters 4.9k Views 3 Watching
  • 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.
  • C Cimmy

    First of all, sorry for my bad english.
    I have a .exe called "rtosim_ik_from_file" which want 3 arguments (model, tracing and taskset) and i run this from the cmd.exe with this command "rtosim_ik_from_file --model OpenSim_Example_3/arm8mark32scaled.osim --trc OpenSim_Example_3/irio/os_irio_11042019.trc --task-set OpenSim_Example_3/irio/ik_taskset.xml -v". Then, it shows a 3d simulation of inverse kinematics.

    Now, i'm building a script which shows a windows with 4 lines: general directory, model's directory, tracing's directory and taskset's directory. By clicking a button, the script has to run the .exe:

    void MainWindow::on_submitpath_clicked()
    {
    QString directory = ui->linepath->text() ;
    QString model = ui->linemodel->text();
    QString tracing = ui->linetracing->text();
    QString taskset = ui->linetaskset->text();
    QString sndstring="rtosim_ik_from_file --model ";
    sndstring.append(model);
    sndstring.append(" --trc ");
    sndstring.append(tracing);
    sndstring.append(" --task-set ");
    sndstring.append(taskset);
    sndstring.append(" -v");
    QStringList params = QStringList() << "/k"<< sndstring;
    qDebug()<<params;
    QString command = "C:/Windows/System32/cmd.exe";
    QProcess process;
    process.setWorkingDirectory(directory);
    process.start(command, params);
    process.waitForFinished(-1);
    }

    But the window freezes and it doesn't work. Any suggestions?
    Thank you in advance.

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by jsulm
    #6

    @Cimmy said in Problem with cmd process:

    process.waitForFinished(-1);

    Why do you wait for it to finish? This is why your app freezes.
    Simply add QProcess member to your class and use it to execute the process without waiting.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    4
    • C Offline
      C Offline
      Cimmy
      wrote on last edited by
      #7

      618bc594-4d2e-4156-97b0-1f2962b716f3-image.png

      First try: use cmd, use rtosim_ik_from_file.exe as parameters. Message "destroyed while process is still running" and nothing happen

      b5ca4651-8541-4a09-9748-2860691ce233-image.png

      Second try: run directly rtosim_ik_from_file.exe with parameters (model, trc ect) but nothing happen. NO "destroyed while [...]" message.

      Any other suggestions?
      Don't know how to create a batch file. I will try...

      jsulmJ 1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #8

        Hi
        -Don't know how to create a batch file. I will try...
        Its just a text file and then you rename it to filename.bat and then its a bat file.

        Regarding the code.
        You are using the parameters in an odd way since you add to a string and then add to that string to the param list.
        Normally you would do like

         QString program = "./path/to/Qt/examples/widgets/analogclock";
         QStringList arguments;
         arguments << "-style" << "fusion";
        

        so each param and value are differnt indexs in the list.
        In your sample, it seems to be one big string

        1 Reply Last reply
        3
        • C Offline
          C Offline
          Cimmy
          wrote on last edited by
          #9

          I'm trying to create a batch file. How can i set the path of this file?

          mrjjM 1 Reply Last reply
          0
          • C Cimmy

            I'm trying to create a batch file. How can i set the path of this file?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #10

            @Cimmy
            Hi Do you mean by the path ?
            You can place it the same place as the .exe if you wish.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Cimmy
              wrote on last edited by
              #11

              void MainWindow::on_submitpath_clicked()
              {
              QString directory = ui->linepath->text() ;
              QString model = ui->linemodel->text();
              QString tracing = ui->linetracing->text();
              QString taskset = ui->linetaskset->text();
              QString sndstring="rtosim_ik_from_file --model ";
              sndstring.append(model);
              sndstring.append(" --trc ");
              sndstring.append(tracing);
              sndstring.append(" --task-set ");
              sndstring.append(taskset);
              sndstring.append(" -v");
              QString filenew = directory;
              filenew.append("/Filenew.bat");
              QFile file(filenew);
              if ( file.open(QIODevice::ReadWrite) )
              {
              QTextStream stream(&file);
              stream << sndstring <<endl;
              }
              QStringList arguments;
              arguments << "/c" << "Filenew.bat";
              QProcess *exec = new QProcess();
              exec->setWorkingDirectory(directory);
              exec->start("cmd.exe", arguments);
              }

              SOLVED!! Thanks to all!!

              jsulmJ 1 Reply Last reply
              1
              • C Cimmy

                618bc594-4d2e-4156-97b0-1f2962b716f3-image.png

                First try: use cmd, use rtosim_ik_from_file.exe as parameters. Message "destroyed while process is still running" and nothing happen

                b5ca4651-8541-4a09-9748-2860691ce233-image.png

                Second try: run directly rtosim_ik_from_file.exe with parameters (model, trc ect) but nothing happen. NO "destroyed while [...]" message.

                Any other suggestions?
                Don't know how to create a batch file. I will try...

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #12

                @Cimmy You're creating local QProcess instance on the stack! It is deleted when the slot finishes!
                I wrote before: "Simply add QProcess member to your class".

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                3
                • C Cimmy

                  void MainWindow::on_submitpath_clicked()
                  {
                  QString directory = ui->linepath->text() ;
                  QString model = ui->linemodel->text();
                  QString tracing = ui->linetracing->text();
                  QString taskset = ui->linetaskset->text();
                  QString sndstring="rtosim_ik_from_file --model ";
                  sndstring.append(model);
                  sndstring.append(" --trc ");
                  sndstring.append(tracing);
                  sndstring.append(" --task-set ");
                  sndstring.append(taskset);
                  sndstring.append(" -v");
                  QString filenew = directory;
                  filenew.append("/Filenew.bat");
                  QFile file(filenew);
                  if ( file.open(QIODevice::ReadWrite) )
                  {
                  QTextStream stream(&file);
                  stream << sndstring <<endl;
                  }
                  QStringList arguments;
                  arguments << "/c" << "Filenew.bat";
                  QProcess *exec = new QProcess();
                  exec->setWorkingDirectory(directory);
                  exec->start("cmd.exe", arguments);
                  }

                  SOLVED!! Thanks to all!!

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #13

                  @Cimmy said in Problem with cmd process:

                  SOLVED!! Thanks to all!!

                  Not really - you leak memory (exec is never deleted). As I wrote already add exec to your class as member, or pointer to QProcess to be able to delete it when you're done.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  4
                  • C Offline
                    C Offline
                    Cimmy
                    wrote on last edited by
                    #14

                    QProcess exec;
                    exec.setWorkingDirectory(directory);
                    exec.start("cmd.exe",arguments);

                    right?

                    JonBJ jsulmJ 3 Replies Last reply
                    0
                    • C Cimmy

                      QProcess exec;
                      exec.setWorkingDirectory(directory);
                      exec.start("cmd.exe",arguments);

                      right?

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #15

                      @Cimmy
                      You need to make the

                      QProcess exec;
                      

                      a member of your class (MainWindow), not a local variable in function on_submitpath_clicked.

                      1 Reply Last reply
                      3
                      • C Cimmy

                        QProcess exec;
                        exec.setWorkingDirectory(directory);
                        exec.start("cmd.exe",arguments);

                        right?

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #16

                        @Cimmy said in Problem with cmd process:

                        right?

                        wrong. Then you will again have same issue: QProcess going out of scope and deleted.
                        I write it now for the third time: "I wrote before: "Simply add QProcess member to your class"." (as @JonB suggested also).

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        4
                        • C Cimmy

                          QProcess exec;
                          exec.setWorkingDirectory(directory);
                          exec.start("cmd.exe",arguments);

                          right?

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #17

                          @Cimmy
                          If you are interested (as I am!) as to why you have things this way. Here is what @jsulm has been telling you:

                          The issue is the QProcess destructor:

                          Destructs the QProcess object, i.e., killing the process.

                          Note that this function will not return until the process is terminated.

                          So if a QProcess gets destructed it will kill the process if it's still running. The problem is your code is only going to start() the sub-process running. It can/will continue running for a while. If your code were waiting for it to finish (e.g. QProcess::execute() or QProcess::waitForFinished()), there wouldn't be a problem, after that you could allow the QProcess to get destroyed.

                          If your QProcess is a local variable on the stack in a function like you propose, as soon as the function exits (variable goes "out of scope") the destructor would get called. So you can either:

                          • Move QProcess exec variable to a member of your class, not destructed till class instance destructed; or

                          • Use QProcess *exec = new QProcess(), allocated on the heap. Not destructed till delete exec. But then you need somewhere to save that pointer so that you can later delete it, no use as a local variable, so equally needs moving to class scope.

                          @jsulm
                          I have musing over this. If you want simple to start a sub-process and "forget" about it (yes, I know about "zombie" processes), this ~QProcess() behaviour is a bit problematic. I don't think startDetached() in itself would help here, it doesn't say that the destructor will not kill the process in this case:

                          If the calling process exits, the detached process will continue to run unaffected.

                          Yes, but if ~QProcess() called on exit it will still kill it, unless the docs are a bit vague here. Perhaps actually it does not? I wonder if QProcess() could do with a setNoKillOrWaitOnDestruct() flag, if startDetached() does not do that?

                          So.... I guess in this case the only safe thing to do would be to go new QProcess and deliberately not delete on exit? C++ doesn't go through everything you've newed and delete prior to exit, does it?! So accept that your program "leaks" prior to exit (e.g. a memory checker) and put up with it?

                          jsulmJ 2 Replies Last reply
                          0
                          • JonBJ JonB

                            @Cimmy
                            If you are interested (as I am!) as to why you have things this way. Here is what @jsulm has been telling you:

                            The issue is the QProcess destructor:

                            Destructs the QProcess object, i.e., killing the process.

                            Note that this function will not return until the process is terminated.

                            So if a QProcess gets destructed it will kill the process if it's still running. The problem is your code is only going to start() the sub-process running. It can/will continue running for a while. If your code were waiting for it to finish (e.g. QProcess::execute() or QProcess::waitForFinished()), there wouldn't be a problem, after that you could allow the QProcess to get destroyed.

                            If your QProcess is a local variable on the stack in a function like you propose, as soon as the function exits (variable goes "out of scope") the destructor would get called. So you can either:

                            • Move QProcess exec variable to a member of your class, not destructed till class instance destructed; or

                            • Use QProcess *exec = new QProcess(), allocated on the heap. Not destructed till delete exec. But then you need somewhere to save that pointer so that you can later delete it, no use as a local variable, so equally needs moving to class scope.

                            @jsulm
                            I have musing over this. If you want simple to start a sub-process and "forget" about it (yes, I know about "zombie" processes), this ~QProcess() behaviour is a bit problematic. I don't think startDetached() in itself would help here, it doesn't say that the destructor will not kill the process in this case:

                            If the calling process exits, the detached process will continue to run unaffected.

                            Yes, but if ~QProcess() called on exit it will still kill it, unless the docs are a bit vague here. Perhaps actually it does not? I wonder if QProcess() could do with a setNoKillOrWaitOnDestruct() flag, if startDetached() does not do that?

                            So.... I guess in this case the only safe thing to do would be to go new QProcess and deliberately not delete on exit? C++ doesn't go through everything you've newed and delete prior to exit, does it?! So accept that your program "leaks" prior to exit (e.g. a memory checker) and put up with it?

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #18

                            @JonB "If the calling process exits, the detached process will continue to run unaffected." - https://doc.qt.io/qt-5/qprocess.html#startDetached
                            So, the QProcess destructor will not terminate the detached process as it is detached.
                            "this ~QProcess() behaviour is a bit problematic" - in what way? If you use startDetached() then the destructor doesn't matter. If you use exec() then I don't see why ~QProcess() terminating process is a problem? At the end it's your job as developer to select the right approach.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            JonBJ 1 Reply Last reply
                            4
                            • jsulmJ jsulm

                              @JonB "If the calling process exits, the detached process will continue to run unaffected." - https://doc.qt.io/qt-5/qprocess.html#startDetached
                              So, the QProcess destructor will not terminate the detached process as it is detached.
                              "this ~QProcess() behaviour is a bit problematic" - in what way? If you use startDetached() then the destructor doesn't matter. If you use exec() then I don't see why ~QProcess() terminating process is a problem? At the end it's your job as developer to select the right approach.

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #19

                              @jsulm

                              "If the calling process exits, the detached process will continue to run unaffected."

                              That describes what happens if the calling process exits. It does not state it countermands what I quoted from ~QProcess(), which states it kills & waits. The question (my question) is what happens, which "wins", if you do not use new but have a "global" scoped QProcess globProc variable (not *globProc), initiate glocProc.startDetached(), and then exit your program. To me the docs are unclear....

                              Can I try this myself? No, because I'm stinky Python, and there are no variables, only heap pointers....

                              jsulmJ 1 Reply Last reply
                              0
                              • JonBJ JonB

                                @Cimmy
                                If you are interested (as I am!) as to why you have things this way. Here is what @jsulm has been telling you:

                                The issue is the QProcess destructor:

                                Destructs the QProcess object, i.e., killing the process.

                                Note that this function will not return until the process is terminated.

                                So if a QProcess gets destructed it will kill the process if it's still running. The problem is your code is only going to start() the sub-process running. It can/will continue running for a while. If your code were waiting for it to finish (e.g. QProcess::execute() or QProcess::waitForFinished()), there wouldn't be a problem, after that you could allow the QProcess to get destroyed.

                                If your QProcess is a local variable on the stack in a function like you propose, as soon as the function exits (variable goes "out of scope") the destructor would get called. So you can either:

                                • Move QProcess exec variable to a member of your class, not destructed till class instance destructed; or

                                • Use QProcess *exec = new QProcess(), allocated on the heap. Not destructed till delete exec. But then you need somewhere to save that pointer so that you can later delete it, no use as a local variable, so equally needs moving to class scope.

                                @jsulm
                                I have musing over this. If you want simple to start a sub-process and "forget" about it (yes, I know about "zombie" processes), this ~QProcess() behaviour is a bit problematic. I don't think startDetached() in itself would help here, it doesn't say that the destructor will not kill the process in this case:

                                If the calling process exits, the detached process will continue to run unaffected.

                                Yes, but if ~QProcess() called on exit it will still kill it, unless the docs are a bit vague here. Perhaps actually it does not? I wonder if QProcess() could do with a setNoKillOrWaitOnDestruct() flag, if startDetached() does not do that?

                                So.... I guess in this case the only safe thing to do would be to go new QProcess and deliberately not delete on exit? C++ doesn't go through everything you've newed and delete prior to exit, does it?! So accept that your program "leaks" prior to exit (e.g. a memory checker) and put up with it?

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #20

                                @JonB And there are static methods in QProcess to execute a process without even creating a QProcess instance.

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                JonBJ 1 Reply Last reply
                                5
                                • JonBJ JonB

                                  @jsulm

                                  "If the calling process exits, the detached process will continue to run unaffected."

                                  That describes what happens if the calling process exits. It does not state it countermands what I quoted from ~QProcess(), which states it kills & waits. The question (my question) is what happens, which "wins", if you do not use new but have a "global" scoped QProcess globProc variable (not *globProc), initiate glocProc.startDetached(), and then exit your program. To me the docs are unclear....

                                  Can I try this myself? No, because I'm stinky Python, and there are no variables, only heap pointers....

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by jsulm
                                  #21

                                  @JonB said in Problem with cmd process:

                                  That describes what happens if the calling process exits

                                  Yes, and if an application exits ~QProcess() will be called (at least if it exits normally)...
                                  It's the whole point of startDetached() - it detaches the QProcess instance from the started process. Just try.

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  3
                                  • jsulmJ jsulm

                                    @JonB And there are static methods in QProcess to execute a process without even creating a QProcess instance.

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on last edited by JonB
                                    #22

                                    @jsulm
                                    Ah!! (And you don't think those create an instance internally?). OK, so if I use static QProcess::startDetached() that really should not call ~QProcess, even on program exit?

                                    It's the whole point of startDetached() - it detaches the QProcess instance from the started process.

                                    Just because a process is detached that does not mean you cannot wait on or kill it, does it? It just means things like it's in its own session.

                                    But it should not matter as the process is detached and the destructor should NOT terminate it.

                                    OK, but I don't get that from the docs! Maybe we read them differently. I'm also having a deeper think about C++ static, too long now of having to do Python... :(

                                    Time for me to have a play....

                                    jsulmJ 1 Reply Last reply
                                    1
                                    • JonBJ JonB

                                      @jsulm
                                      Ah!! (And you don't think those create an instance internally?). OK, so if I use static QProcess::startDetached() that really should not call ~QProcess, even on program exit?

                                      It's the whole point of startDetached() - it detaches the QProcess instance from the started process.

                                      Just because a process is detached that does not mean you cannot wait on or kill it, does it? It just means things like it's in its own session.

                                      But it should not matter as the process is detached and the destructor should NOT terminate it.

                                      OK, but I don't get that from the docs! Maybe we read them differently. I'm also having a deeper think about C++ static, too long now of having to do Python... :(

                                      Time for me to have a play....

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #23

                                      @JonB said in Problem with cmd process:

                                      And you don't think those create an instance internally?

                                      I don't know. But it should not matter as the process is detached and the destructor should NOT terminate it.

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      JonBJ 1 Reply Last reply
                                      1
                                      • jsulmJ jsulm

                                        @JonB said in Problem with cmd process:

                                        And you don't think those create an instance internally?

                                        I don't know. But it should not matter as the process is detached and the destructor should NOT terminate it.

                                        JonBJ Offline
                                        JonBJ Offline
                                        JonB
                                        wrote on last edited by JonB
                                        #24

                                        @jsulm
                                        Just to confirm your interpretation.

                                        From Python/PySide2, from a terminal if I run an interactive python3 and do

                                        >>> from PySide2.QtCore import QProcess
                                        >>> p = QProcess(); p.start("./script")
                                        

                                        and then exit the python session (python will auto-delete everything created), I get a message

                                        QProcess: Destroyed while process ("./script") is still running.
                                        

                                        But if I use

                                        >>> p = QProcess(); p.startDetached("./script")
                                        # or
                                        >>> QProcess.startDetached("./script")
                                        

                                        no message, and I continue to see ./script's output after the python session has exited.

                                        jsulmJ 1 Reply Last reply
                                        1
                                        • JonBJ JonB

                                          @jsulm
                                          Just to confirm your interpretation.

                                          From Python/PySide2, from a terminal if I run an interactive python3 and do

                                          >>> from PySide2.QtCore import QProcess
                                          >>> p = QProcess(); p.start("./script")
                                          

                                          and then exit the python session (python will auto-delete everything created), I get a message

                                          QProcess: Destroyed while process ("./script") is still running.
                                          

                                          But if I use

                                          >>> p = QProcess(); p.startDetached("./script")
                                          # or
                                          >>> QProcess.startDetached("./script")
                                          

                                          no message, and I continue to see ./script's output after the python session has exited.

                                          jsulmJ Offline
                                          jsulmJ Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #25

                                          @JonB said in Problem with cmd process:

                                          no message, and I continue to see ./script's output after the python session has exited.

                                          This is expected, isn't it? As stated in the documentation. ~QProcess() is called in both cases.

                                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          JonBJ 1 Reply Last reply
                                          1

                                          • Login

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