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. How to correctly close the subroutine started by QProcess?
Forum Updated to NodeBB v4.3 + New Features

How to correctly close the subroutine started by QProcess?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 181 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.
  • A Offline
    A Offline
    alexaatm
    wrote on 9 Feb 2024, 13:28 last edited by
    #1

    I stumbled on the same issue as this one and found a solution:
    Re: How to close the subroutine of the program started by qprocess?. The ticket was marked as solved, however, I did not find a straightforward solution, and Qt forum advised me to open a new ticket.

    Problem: Qt Process I launched under the hood launches other executable. After calling process.terminate(), the other executable was still active.

    QProcess: Destroyed while process PATH_TO_OTHER_EXE is still running.
    

    The solution is to call

    process.waitForFinished()
    

    (30sec timeout) or

    process.waitForFinished(-1)
    

    (waits untill closes), which waits for the subroutine to exit too. Ref: https://stackoverflow.com/questions/14504201/qprocess-and-shell-destroyed-while-process-is-still-running

    After that, my issue was resolved, and I did not have to manually kill the started subroutine every time.

    Hope this will be useful for somebody too:)

    A 1 Reply Last reply 9 Feb 2024, 18:07
    0
    • A alexaatm has marked this topic as solved on 9 Feb 2024, 13:29
    • A alexaatm
      9 Feb 2024, 13:28

      I stumbled on the same issue as this one and found a solution:
      Re: How to close the subroutine of the program started by qprocess?. The ticket was marked as solved, however, I did not find a straightforward solution, and Qt forum advised me to open a new ticket.

      Problem: Qt Process I launched under the hood launches other executable. After calling process.terminate(), the other executable was still active.

      QProcess: Destroyed while process PATH_TO_OTHER_EXE is still running.
      

      The solution is to call

      process.waitForFinished()
      

      (30sec timeout) or

      process.waitForFinished(-1)
      

      (waits untill closes), which waits for the subroutine to exit too. Ref: https://stackoverflow.com/questions/14504201/qprocess-and-shell-destroyed-while-process-is-still-running

      After that, my issue was resolved, and I did not have to manually kill the started subroutine every time.

      Hope this will be useful for somebody too:)

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 9 Feb 2024, 18:07 last edited by
      #2

      @alexaatm That sounds like a horrible solution as this will block your event loop.

      Let me guess: you create the QProcess object on the stack and when the object goes out of scope, it is destroyed and therefore the executable is killed.

      The correct solution is to make the QProcess a member variable so it stays alive when the calling function exits.

      Instead the waitForXxxx functions you should use signals and slots to communicate with the process.

      Regards

      Qt has to stay free or it will die.

      1 Reply Last reply
      1

      2/2

      9 Feb 2024, 18:07

      • Login

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