Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) QtSignalExample3

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppQtSignalExample3/CppQtSignalExample3.pro

 

include(../../DesktopApplication.pri)

SOURCES += \
    main.cpp \
    receiver.cpp

HEADERS += \
    receiver.h

 

 

 

 

 

./CppQtSignalExample3/main.cpp

 

#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 <iostream>

#include <QApplication>
#include <QDoubleSpinBox>

#include "receiver.h"
#pragma GCC diagnostic pop

int main(int argc,char* argv[])
{
  QApplication a(argc,argv);

  QDoubleSpinBox b;
  Receiver r;
  //QObject::connect(&b,SIGNAL(valueChanged(double)),&r,SLOT(OnReceive(double))); //Qt4
  QObject::connect(&b,
    static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
    &r,
    static_cast<void (Receiver::*)(double) const>(&Receiver::OnReceive)
  ); //Qt5
  b.setValue(1.1);
  b.setValue(2.2);


  a.exit(); //To satisfy the compiler
  return 0; //To satisfy the compiler
}

/* Screen output:

Receiver: received signal: a double with value 1.1
Receiver: received signal: a double with value 2.2

*/

 

 

 

 

 

./CppQtSignalExample3/receiver.h

 

#ifndef RECEIVER_H
#define RECEIVER_H

#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 <QObject>
#pragma GCC diagnostic pop

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

public slots:
  void OnReceive() const noexcept;
  void OnReceive(const double x) const noexcept;
};

#endif // RECEIVER_H

 

 

 

 

 

./CppQtSignalExample3/receiver.cpp

 

#include "receiver.h"

#include <iostream>

Receiver::Receiver(QObject *parent)
  : QObject(parent)
{

}

void Receiver::OnReceive() const noexcept
{
  std::clog << "Receiver: received signal" << std::endl;
}

void Receiver::OnReceive(const double x) const noexcept
{
  std::clog << "Receiver: received signal: a double with value " << x << std::endl;
}

 

 

 

 

 

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