Lecko
Lecko

Reputation: 1385

Qt handle process termination

I have a Qt application that starts another application. I want to receive a signal, if the child application is terminated externally.

The code is:

CaptureApp::CaptureApp(int& argc, char** argv): QApplication(argc, argv)
{    
    launchDaemon();
}

void CaptureApp::launchDaemon()
{
    QString command = "daemon";
    QStringList arguments;
    arguments << "somearg";
    process = new QProcess(this);
    process->start(command, arguments);
    connect(process,SIGNAL(stateChanged(QProcess::ProcessState)),this,SLOT(daemonDied(QProcess::ProcessState)));
    connect(process,SIGNAL(finished(int)),this,SLOT(daemonDied(int)));
}
void CaptureApp::daemonDied(QProcess::ProcessState state)
{
    std::cout << "DAEMON DIED" << std::endl;
}

void CaptureApp::daemonDied(int code)
{
    std::cout << "DAEMON DIED" << std::endl;
}

But no message appears when I kill child process. What am I doing wrong?

Upvotes: 1

Views: 865

Answers (1)

agold
agold

Reputation: 6276

I tried your example inherriting from QApplication, but in the first place it gave me this error:

QObject::connect: No such slot QApplication::daemonDied(QProcess::ProcessState)
QObject::connect: No such slot QApplication::daemonDied(int)

I then added Q_OBJECT to the class definition and it connected the signals/slots, but when I killed the process it gave the following error:

ICE default IO error handler doing an exit(), pid = 27773, errno = 4

When I however changed the class to inherit from QObject instead of QApplication it did work. So having the main:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    CaptureApp captureApp(argc,argv);

    return a.exec();
}

As commented by jbm, some processes stay attached, while others are not. I tried first gedit and it received a signal directly after starting indicating the processes had finished. Using vim however it did keep running and I was able to kill it externally.

Upvotes: 1

Related Questions