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. Is this an efficient use of QProcess?
Qt 6.11 is out! See what's new in the release blog

Is this an efficient use of QProcess?

Scheduled Pinned Locked Moved Solved General and Desktop
26 Posts 4 Posters 18.4k Views 1 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.
  • G Offline
    G Offline
    graniteDev
    wrote on last edited by
    #12

    I wonder if the confusion is that I left out the beginning code, the first process starts out:

    QProcess *process = new QProcess(this);
    process->execute(file1,args1);
    process->waitforFinish();
    process->close();
    
    process = new QProcess(this);
    process->execute(file2,args2);
    process->waitforFinish();
    process->close();
    
    process = new QProcess(this);
    process->execute(file3,args3);
    process->waitforFinish();
    process->close();
    

    I get no compile errors, and it's working. Per the Qt documentation close() deletes the object, so I don't believe I'm creating a memory leak with what I'm doing.

    However, if I can eliminate creating a new object and then deleting it every time, I'm for improving the code.

    1 Reply Last reply
    0
    • SGaistS SGaist

      waitForFinished has a timeout with a default value but in this case, since the process is not even running it will return early.

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

      @SGaist
      Ah, OK, I thought it might error "process not yet started".

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #14

        @graniteDev Warning doesn't mean error unless you enabled "all warning be errors" for your compiler. As for close, it doesn't delete anything. It will kill the process that you started and close the communication channels. It's unrelated to memory management.

        @JonB waitForFinished will return false because of that.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        G 1 Reply Last reply
        0
        • SGaistS SGaist

          @graniteDev Warning doesn't mean error unless you enabled "all warning be errors" for your compiler. As for close, it doesn't delete anything. It will kill the process that you started and close the communication channels. It's unrelated to memory management.

          @JonB waitForFinished will return false because of that.

          G Offline
          G Offline
          graniteDev
          wrote on last edited by
          #15

          @SGaist Ok, how should I go about this properly then? Could you provide some example code?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #16
            QProcess::execute(file1, args1);
            

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            G 1 Reply Last reply
            2
            • SGaistS SGaist
              QProcess::execute(file1, args1);
              
              G Offline
              G Offline
              graniteDev
              wrote on last edited by
              #17

              @SGaist I could run these from a static function? How then do I know when they are finished if there is no object to emit the signal?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #18

                Did you read the documentation of the function ? execute runs the command and waits for it to finish. The returned value indicates what happened.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                G 1 Reply Last reply
                3
                • SGaistS SGaist

                  Did you read the documentation of the function ? execute runs the command and waits for it to finish. The returned value indicates what happened.

                  G Offline
                  G Offline
                  graniteDev
                  wrote on last edited by
                  #19

                  @SGaist yes but if i just run QProcess::execute(file1, args1); there is no object there for me to deal with.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #20

                    What do you want to deal with ? In the code you shown until now, what you are doing with your QProcess objects is already done "internally" by execute.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    G 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      What do you want to deal with ? In the code you shown until now, what you are doing with your QProcess objects is already done "internally" by execute.

                      G Offline
                      G Offline
                      graniteDev
                      wrote on last edited by
                      #21

                      @SGaist never mind, I misunderstood because I'm not used to dealing with these. I was concerned that the subsequent processes could be started before the previous one finished, however execute is not asynchronous like start() is if I understood the documentation correctly.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #22

                        It is indeed not asynchronous.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        G 2 Replies Last reply
                        0
                        • SGaistS SGaist

                          It is indeed not asynchronous.

                          G Offline
                          G Offline
                          graniteDev
                          wrote on last edited by
                          #23

                          @SGaist ok well thankyou, I will give this a try.

                          1 Reply Last reply
                          0
                          • SGaistS SGaist

                            It is indeed not asynchronous.

                            G Offline
                            G Offline
                            graniteDev
                            wrote on last edited by
                            #24

                            @SGaist Worked. Wow is that much much simpler.

                            If I may though, for my future reference, if I needed to actually use a QProcess *process = new QProcess(this); to run multiple things, how would I go about it?

                            JonBJ 1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #25

                              If in a fashion similar to execute see the Synchronous Process API in QProcess's documentation.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              1
                              • G graniteDev

                                @SGaist Worked. Wow is that much much simpler.

                                If I may though, for my future reference, if I needed to actually use a QProcess *process = new QProcess(this); to run multiple things, how would I go about it?

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

                                @graniteDev said in Is this an efficient use of QProcess?:

                                If I may though, for my future reference, if I needed to actually use a QProcess *process = new QProcess(this); to run multiple things, how would I go about it?

                                Exactly as per your original code, except change execute() to start().

                                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