How to catch CTRL+C in linux based system
-
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);@ -
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);
}
@