GithubHelp home page GithubHelp logo

qt-unix-signals's Introduction

Unix Signal Watcher For Qt

Author: Simon Knopp
Licence: MIT

Summary

When writing a Qt application, as with any application, it is sometimes useful to handle Unix signals. Of course, Qt already incorporates the notion of signals, so it would be nice if Unix signals could be mapped to Qt signals. Then we could write handlers for Unix signals and connect them up in the same way as normal Qt slots.

The class described below does just this. It is heavily based on this example in the Qt documentation, but it encapsulates that functionality in a generic re-usable class.

Interface

The interface is simple: you call watchForSignal() with the signals you're interested in, and connect() your handlers to SIGNAL(unixSignal(int)).

class UnixSignalWatcher : public QObject
{
    Q_OBJECT
public:
    explicit UnixSignalWatcher(QObject *parent = 0);
    ~UnixSignalWatcher();

    void watchForSignal(int signal);

signals:
    void unixSignal(int signal);
};

Example usage

Let's look at an example program.

#include <QCoreApplication>
#include <QDebug>
#include "sigwatch.h"

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    qDebug() << "Hello from process" << QCoreApplication::applicationPid();

    UnixSignalWatcher sigwatch;
    sigwatch.watchForSignal(SIGINT);
    sigwatch.watchForSignal(SIGTERM);
    QObject::connect(&sigwatch, SIGNAL(unixSignal(int)), &app, SLOT(quit()));

    int exitcode = app.exec();
    qDebug() << "Goodbye";
    return exitcode;
}

This simply registers signal handlers for SIGINT and SIGTERM and then idles forever. If you run it (qmake && make && ./sigwatch-demo) you'll see a greeting and the pid of the process:

Hello from process 6811

Press ^C to send SIGINT. The UnixSignalWatcher will handle the signal, which in turn is connected to QCoreApplication::quit(), so the event loop exits and the farewell message is printed.

^CCaught signal: Interrupt 
Goodbye

Similarly, you could use kill to send SIGTERM.

$ ./sigwatch-demo &
Hello from process 6848
$ kill 6848
Caught signal: Terminated
Goodbye

If you send a signal that does not have a handler, though, you won't see the farewell message. For instance:

$ ./sigwatch-demo
Hello from process 6906
$ kill -SIGABRT 6906
Aborted (core dumped)

Compatibility

Tested with Qt 4.6 and 5.2 on Linux.

qt-unix-signals's People

Contributors

sijk avatar kosmaz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.