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 use "sigaction" in Qt?
Forum Updated to NodeBB v4.3 + New Features

How to use "sigaction" in Qt?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 9.1k 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.
  • Y Offline
    Y Offline
    yaoyulian
    wrote on 18 May 2011, 07:54 last edited by
    #1

    @fd = open("/dev/hpi0",O_RDONLY);
    sigaction(SIGIO, sig_handler, NULL);
    fcntl(fd, F_SETOWN, getpid());
    fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | FASYNC);@
    This can't work.
    How to use "sigaction" in -QT- Qt? Or, other method to achieve "soft interrupt"?
    Thanks!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alexander
      wrote on 18 May 2011, 07:58 last edited by
      #2

      I guess, it's not qt sphere.

      1 Reply Last reply
      0
      • ? This user is from outside of this forum
        ? This user is from outside of this forum
        Guest
        wrote on 18 May 2011, 08:07 last edited by
        #3

        Maybe this? (it does work for SIGSEGV and SIGINT, I often get those)
        @
        struct sigaction act, oact;
        memset((void*)&act, 0, sizeof(struct sigaction));
        memset((void*)&oact, 0, sizeof(struct sigaction));
        act.sa_flags = 0;
        act.sa_handler = &signalHandler;
        sigaction(SIGINT, &act, &oact);
        sigaction(SIGKILL, &act, &oact);
        sigaction(SIGQUIT, &act, &oact);
        sigaction(SIGSTOP, &act, &oact);
        sigaction(SIGTERM, &act, &oact);@

        (...)

        @
        void signalHandler(int signal)
        {
        //print incoming signal
        switch(signal){
        case SIGINT: fprintf(stderr, "SIGINT => "); break;
        case SIGKILL: fprintf(stderr, "SIGKILL => "); break;
        case SIGQUIT: fprintf(stderr, "SIGQUIT => "); break;
        case SIGSTOP: fprintf(stderr, "SIGSTOP => "); break;
        case SIGTERM: fprintf(stderr, "SIGTERM => "); break;
        case SIGSEGV: fprintf(stderr, "SIGSEGV => "); break;
        default: fprintf(stderr, "APPLICATION EXITING => "); break;
        }

        //...
        

        }
        @

        Hope it helps!!

        --

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          yaoyulian
          wrote on 18 May 2011, 08:07 last edited by
          #4

          @class MainWindow : public QMainWindow
          {
          Q_OBJECT
          private slots:
          void sigterm_handler(int);
          };

          MainWindow::MainWindow(QWidget parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
          {
          ui->setupUi(this);
          struct sigaction action;
          fd = open("/dev/hpi0",O_RDONLY);
          memset(&action, 0, sizeof(action));
          action.sa_handler = sigterm_handler;
          action.sa_flags = 0;
          sigaction(SIGIO, &action, NULL);
          fcntl(fd, F_SETOWN, getpid());
          fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | FASYNC);
          }@
          error at "action.sa_handler = sigterm_handler;"
          message: error: argument of type 'void (MainWindow::)(int)' does not match 'void (
          )(int)'

          1 Reply Last reply
          0
          • ? This user is from outside of this forum
            ? This user is from outside of this forum
            Guest
            wrote on 18 May 2011, 08:14 last edited by
            #5

            man sigaction

            --

            1 Reply Last reply
            0
            • S Offline
              S Offline
              situ117
              wrote on 18 May 2011, 21:47 last edited by
              #6

              Hi,

              It's not possible to directly call a non static member function of class through C style signal handler. Try to delegate task of handling signal to an instance of class using a static member function. See code below for example:

              @
              #include <iostream>
              #include <signal.h>

              using namespace std;

              class MyClass {
              static MyClass* m;

              public:

              void handleSignal(int num){
              cout<<"Signal handled: "<<num<<endl;
              }

              static void setSignalHandlerObject(MyClass* mc) {
              m = mc;
              }

              static void callSignalHandler(int num){
              m->handleSignal(num);
              }
              };

              MyClass* MyClass::m = NULL;

              int main(){
              signal(SIGINT,MyClass::callSignalHandler);
              while(1);
              }
              @

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dangelog
                wrote on 18 May 2011, 22:15 last edited by
                #7

                Use the Self Pipe Trick.

                http://doc.qt.nokia.com/4.7/unix-signals.html

                Apart from this, UNIX signal handlers must be ordinary C functions (in C++ you'll need the extern C linkage).

                Software Engineer
                KDAB (UK) Ltd., a KDAB Group company

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  DungeonLords
                  wrote on 2 Mar 2023, 05:35 last edited by
                  #8

                  This example explain how to use SIGINT (terminate app using Ctrl+C).

                  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