How to use "sigaction" in Qt?

  • @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"?

  • I guess, it's not qt sphere.

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


  • @class MainWindow : public QMainWindow
    private slots:
    void sigterm_handler(int);

    MainWindow::MainWindow(QWidget parent) :
    ui(new Ui::MainWindow)
    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 (

  • man sigaction


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


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

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

    static void callSignalHandler(int num){

    MyClass* MyClass::m = NULL;

    int main(){

  • Use the Self Pipe Trick.

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

Log in to reply

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