How to catch CTRL+C in linux based system
-
wrote on 2 May 2012, 06:00 last edited by
Hello,
I need to catch CTRL+C in my program.
is there any simple method like I simply write a slot for this signal and just connect them.??
[in console application] -
wrote on 2 May 2012, 07:07 last edited by
There is:
@QKeySequence keys(QKeySequence::Copy);
QAction * action_copy= new QAction(this);
action_copy->setShortcut(keys);
connect(action_copy, SIGNAL(triggered()),this,SLOT(copy_whatever()));
this->addAction(action_copy);@ -
wrote on 27 Feb 2013, 12:37 last edited by
To terminate the QApplication loop you can call the static method QApplication::exit().
So all we have to do is catch the signal which Linux sends when you press ctrl-c, and then call exit() from the handler.@
void setShutDownSignal( int signalId );
void handleShutDown( int signalId );int main(int argc, char **argv)
{
setShutDownSignal( SIGINT ); // shut down on ctrl-c
setShutDownSignal( SIGTERM ); // shut down on killallQApplication::exec();
}void setShutDownSignal( int signalId )
{
struct sigaction sa;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sa.sa_handler = handleShutDownSignal;
if (sigaction(signalId, &sa, NULL) == -1)
{
perror("setting up termination signal");
exit(1);
}
}void handleShutDownSignal( int /signalId/ )
{
QApplication::exit(0);
}
@