Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) BoostSignals2Example1

 

BoostQt CreatorLubuntu

 

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.

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

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

 

 

 

 

 

./CppBoostSignals2Example1/emitter.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

 

 

 

 

 

./CppBoostSignals2Example1/emitter.cpp

 

#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";
}

 

 

 

 

 

./CppBoostSignals2Example1/main.cpp

 

#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();
  }
}

 

 

 

 

 

./CppBoostSignals2Example1/receiver.h

 

#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

 

 

 

 

 

./CppBoostSignals2Example1/receiver.cpp

 

#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";
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml