How to catch CTRL+C in linux based system

  • 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]

  • There is:

    @QKeySequence keys(QKeySequence::Copy);
    QAction * action_copy= new QAction(this);
    connect(action_copy, SIGNAL(triggered()),this,SLOT(copy_whatever()));

  • 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 killall


    void setShutDownSignal( int signalId )
    struct sigaction sa;
    sa.sa_flags = 0;
    sa.sa_handler = handleShutDownSignal;
    if (sigaction(signalId, &sa, NULL) == -1)
    perror("setting up termination signal");

    void handleShutDownSignal( int /signalId/ )

  • Hi!

    In the last post, It looks like the second line should be
    @void handleShutDownSignal( int signalId );@

    instead of

    @ void handleShutDown( int signalId );@

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.