Search code examples
pythonpython-3.xpyqtpyqt4qprocess

QProcess exit normally


I'm using python with qt and i cannot find out a way to fire a signal when Qprocess exit normally, According to Pyqt documentation finished() signal can take 2 arguments exitCode and exitStatus

This is what Pyqt documentation says about finished() signal

http://pyqt.sourceforge.net/Docs/PyQt4/qprocess.html#finished

void finished (int, ::QProcess::ExitStatus)

This is the default overload of this signal.

This signal is emitted when the process finishes. exitCode is the exit code of the process, and exitStatus is the exit status. After the process has finished, the buffers in QProcess are still intact. You can still read any data that the process may have written before it finished.

QProcess.ExitStatus

This enum describes the different exit statuses of QProcess.

Constant..................Value.........Description

QProcess.NormalExit....... 0.......The process exited normally.

QProcess.CrashExit........ 1.......The process crashed.

I tried to use this syntax but it did't work

self.process.finished(0,QProcess_ExitStatus=0).connect(self.status)

Remark:

Status is just as symbol for any slot (any action ) not something specific

Update:

To get a sense of the problem I've more than one process (Think of it as queue) i need python to execute the first process and only move to the next one if the previous process exits normally not forced to exit using kill() or terminate()

Thanks in advance


Solution

  • You do not have to point to the symbol in the connection but in the slot with the help of pyqtSlot.

    from PyQt4 import QtCore
    
    class Helper(QtCore.QObject):
        def __init__(self, parent=None):
            super(Helper, self).__init__(parent)
            self.process = QtCore.QProcess(self)
            self.process.start("ping -c 4 google.com")
            self.process.finished.connect(self.status)
    
        @QtCore.pyqtSlot(int, QtCore.QProcess.ExitStatus)
        def status(self, exitCode, exitStatus):
            print(exitCode, exitStatus)
            QtCore.QCoreApplication.quit()
    
    if __name__ == '__main__':
        import sys
        app = QtCore.QCoreApplication(sys.argv)
        h = Helper()
        sys.exit(app.exec_())
    

    Update:

    from PyQt4 import QtCore
    
    class Helper(QtCore.QObject):
        def __init__(self, parent=None):
            super(Helper, self).__init__(parent)
            self.process = QtCore.QProcess(self)
            self.process.start("ping -c 4 google.com")
            self.process.finished.connect(self.on_finished)
    
        @QtCore.pyqtSlot(int, QtCore.QProcess.ExitStatus)
        def on_finished(self, exitCode, exitStatus):
            if exitStatus == QtCore.QProcess.NormalExit:
                self.status()
    
        def status(self):
            print("status")