Boost.Signals2 example 1: comparing Boost and Qt is a Boost.Signals2 example that shows both the use of Boost.Signals2 signals and Qt signals.
The article From Qt signal to Boost signal describes why and how to move from using Qt signals to using Boost.Signal2 signals.
Operating system(s) or programming environment(s)
- Lubuntu 15.04 (vivid)
- Qt Creator 3.1.1
- G++ 4.9.2
Libraries used:
- STL: GNU ISO C++ Library, version 4.9.2
Qt project file: ./CppBoostSignals2Example1/CppBoostSignals2Example1.pro
include(../../ConsoleApplication.pri) include(../../Libraries/Boost.pri) SOURCES += \ main.cpp \ emitter.cpp \ receiver.cpp HEADERS += \ emitter.h \ receiver.h
#ifndef EMITTER_H #define EMITTER_H #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <QObject> #pragma GCC diagnostic pop class QtEmitter : public QObject { Q_OBJECT public: explicit QtEmitter(QObject *parent = 0); void DoEmit(); signals: void signal_emit(); }; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #pragma GCC diagnostic ignored "-Wunused-but-set-parameter" #include <boost/signals2.hpp> #pragma GCC diagnostic pop struct Emitter { boost::signals2::signal<void ()> m_signal; void DoEmit(); }; #endif // EMITTER_H
#include <iostream> #include "emitter.h" QtEmitter::QtEmitter(QObject *parent) : QObject(parent) { } void QtEmitter::DoEmit() { std::clog << "QtEmitter: emitting signal\n"; emit signal_emit(); } void Emitter::DoEmit() { m_signal(); std::clog << "Emitter: emitting signal\n"; }
#pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <boost/signals2.hpp> #include "emitter.h" #include "receiver.h" #pragma GCC diagnostic pop int main() { { //The Qt way QtEmitter e; QtReceiver r; QObject::connect(&e,SIGNAL(signal_emit()),&r,SLOT(OnReceive())); e.DoEmit(); } { //The Boost way Emitter e; Receiver r; e.m_signal.connect( boost::bind( &Receiver::OnReceive, r)); e.DoEmit(); } }
#ifndef RECEIVER_H #define RECEIVER_H #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <QObject> #pragma GCC diagnostic pop class QtReceiver : public QObject { Q_OBJECT public: explicit QtReceiver(QObject *parent = 0); signals: public slots: void OnReceive(); }; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <boost/signals2.hpp> #pragma GCC diagnostic pop struct Receiver { void OnReceive(); }; #endif // RECEIVER_H
#include <iostream> #include "receiver.h" QtReceiver::QtReceiver(QObject *parent) : QObject(parent) { } void QtReceiver::OnReceive() { std::clog << "QtReceiver: received signal\n"; } void Receiver::OnReceive() { std::clog << "Receiver: received signal\n"; }