Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to catch CTRL+C in linux based system
Forum Updated to NodeBB v4.3 + New Features

How to catch CTRL+C in linux based system

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 8.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aekam
    wrote on last edited by
    #1

    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]

    If you take care of inches, you won't have to worry about miles... :)

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AcerExtensa
      wrote on last edited by
      #2

      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);@

      God is Real unless explicitly declared as Integer.

      1 Reply Last reply
      0
      • H Offline
        H Offline
        HenrikNB
        wrote on last edited by
        #3

        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

        QApplication::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);
        }
        @

        1 Reply Last reply
        0
        • G Offline
          G Offline
          Ganton
          wrote on last edited by
          #4

          Hi!

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

          instead of

          @ void handleShutDown( int signalId );@

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved