Appendix C. Creating your own signals
Prev |  | Next

## Appendix C. Creating your own signals

Now that you've seen signals and signal handlers in gtkmm, you might like to use the same technique to allow interaction between your own classes. That's actually very simple by using the libsigc++ library directly.

This isn't purely a gtkmm or GUI issue. gtkmm uses libsigc++ to implement its proxy wrappers for the GTK+ signal system, but for new, non-GTK+ signals, you can create pure C++ signals, using the SigC::Signal*<> templates.

For instance, to create a signal that sends 2 parameters, a bool and an integer, just declare a SigC::Signal2, like so:

    SigC::Signal<void, bool int> signal_something;

You could just declare that signal as a public member variable, but some people find that distasteful and prefer to make it available via an accessor method, like so:

    class Server
    {
      //signal accessor:
      typedef SigC::Signal2<void, bool, int> type_signal_something;
      type_signal_something signal_something();

    protected:
      type_signal_something m_signal_something;
    };

    Server::type_signal_something Server::signal_something()
    {
      return m_signal_something;
    }

You can then connect to the signal using the same syntax used when connecting to gtkmm signals. For instance,

    server.signal_something().connect(
      SigC::slot(client, &Client::on_server_something) );

See examples/book/signals/custom/ for a full working example.

Prev | Up | Next
X Event signals | Home | Appendix D. Comparison with other signalling systems