Frederik Petri
Frederik Petri

Reputation: 481

How to cleanly exit a QProcess with Ctrl+C input?

I am running a QProcess event that exits when pushing Ctrl+C directly in cmd. How can I send a signal from python to close down the process cleanly? I've tryed kill() which doesn't let the program save accordingly. Also terminate() doesn't respond. I hope you can help.

    self.btn_1 = QPushButton('Start')
    self.btn_1.clicked.connect(self.start)

    self.btn_2 = QPushButton('Stop')
    self.btn_1.clicked.connect(self.stop)

    self.process = QProcess()

def start(self):
    self.process.start("C:\path\program.exe")

def stop(self):
    print('Stop the process!')

Upvotes: 0

Views: 1338

Answers (1)

mguijarr
mguijarr

Reputation: 7920

You need to get the process PID then you can send the signal via os.kill:

import os
import signal 

os.kill(<pid>, signal.SIGINT) #SIGINT is CTRL-C

You get the PID from the QProcess object with:

pid = self.process.processId()

EDIT: on Windows, replace SIGINT with CTRL_C_EVENT

Upvotes: 1

Related Questions