0

I'm trying to get the PID of a finished QProcess, something like this:

proc = QProcess.start()
proc.finished.connect(self.finished)

def finished(self):
    self.sender().pid()

QProcess.pid() will return 0 if the process is closed however, and QProcess.finished() would only call once the process is complete, so I can only ever get 0... How can I work around this? I'm trying to think of a way to NOT have to use the PID on completion, but at the moment that's what I have to do.

I tried proc.aboutToClose.connect(self.finished) but this never returned anything so it seems it wasn't signaled, but that would be a good solution if I can get that to work.

Spencer
  • 1,931
  • 1
  • 21
  • 44
  • Use the `started()` signal to get the pid. – ekhumoro Mar 15 '17 at 03:52
  • Thanks, but I'm trying to get it at the end of the process. What I'm doing is updating a QTableView item to say "Completed" when the process completes and "Running..." while the process runs, etc. I use the PID to know which process has completed (there are an arbitrary number running at a time) – Spencer Mar 15 '17 at 04:45
  • 1
    That makes no sense. By definition, a completed process does not have a pid. Use the `started()` signal to cache the pid on the process object (e.g. `proc.setObjectName(str(proc.pid()))`). – ekhumoro Mar 15 '17 at 05:39
  • Nice that's exactly what I needed! I know it makes no sense (which is why I basically said so above) but I knew nothing about `setObjectName`. It's just what I need! – Spencer Mar 15 '17 at 06:46

1 Answers1

1

Ekhumoro gave the answer, the solution is to name the process by the PID and therefore you can still get it later by just querying the object name:

proc.setObjectName(str(proc.pid()))

Spencer
  • 1,931
  • 1
  • 21
  • 44