C++ (Qt)#include <QObject>#include <iostream> class BaseQtShape : public QObject{ Q_OBJECT public: void animate() { emit visibleChanged(true); emit enabledChanged(false); emit positionChanged(5, 7); emit sizeChanged(10, 15); } signals: void visibleChanged(bool visible); void enabledChanged(bool enable); void positionChanged(int x, int y); void sizeChanged(int height, int width);}; class ChildQtShape : public BaseQtShape{ Q_OBJECT public: void animate() { BaseQtShape::animate(); emit scaleChanged(3, 8); } signals: void scaleChanged(int x, int y);}; class BaseQtLogger : public QObject{ Q_OBJECT public slots: void onVisibleChanged(bool visible) { std::cout << "visibleChanged: " << visible << std::endl; } void onEnabledChanged(bool enable) { std::cout << "enabledChanged: " << enable << std::endl; } void onPositionChanged(int x, int y) { std::cout << "positionChanged: " << x << ", " << y << std::endl; } void onSizeChanged(int height, int width) { std::cout << "sizeChanged: " << height << ", " << width << std::endl; }}; class ChildQtLogger : public BaseQtLogger{ Q_OBJECT public slots: void onScaleChanged(int x, int y) { std::cout << "scaleChanged: " << x << ", " << y << std::endl; }}; int main(int /* argc */, char ** /* argv[] */){ ChildQtShape shape; ChildQtLogger logger; QObject::connect(&shape, &BaseQtShape::visibleChanged, &logger, &BaseQtLogger::onVisibleChanged); QObject::connect(&shape, &BaseQtShape::enabledChanged, &logger, &BaseQtLogger::onEnabledChanged); QObject::connect(&shape, &BaseQtShape::positionChanged, &logger, &BaseQtLogger::onPositionChanged); QObject::connect(&shape, &BaseQtShape::sizeChanged, &logger, &BaseQtLogger::onSizeChanged); QObject::connect(&shape, &ChildQtShape::scaleChanged, &logger, &ChildQtLogger::onScaleChanged); shape.animate(); // Invalid usage. emit must be protected. shape.emit visibleChanged(true); return 0;} #include "main.moc"
protected: void EmitVisibleChanged(bool b) { emit visibleChanged(b); }
// Invalid usage. emit must be protected. shape.emit visibleChanged(true);
C++ (Qt)#include <iostream>#include <boost/signals2.hpp> using namespace std; void func( int val1, int val2 ){ cout << "Call func: " << val1 << " " << val2 << endl;} #define DEF_SIGNAL(name, type) \public: \ template<typename Slot> \ void connect_##name( Slot slot ) \ { \ m_##name.connect( slot ); \ } \ \ template<typename Slot> \ void disconnect_##name( Slot slot ) \ { \ m_##name.disconnect( slot ); \ } \private: \ boost::signals2::signal<type> m_##name; \ class Demo{public: explicit Demo() {} void emit( int val ) { cout << "Emit signal" << endl; m_signal( val, val ); } DEF_SIGNAL(signal, void( int, int ) )}; int main(){ cout << "Start..." << endl; Demo demo; demo.connect_signal( &func ); demo.emit( 10 ); return 0;}
C++ (Qt)template <class, class>class signal; template <class Friend, class R, class... Args>class signal<Friend, R(Args...)> {public: template <class Slot> void connect(Slot slot) { m_sig.connect(slot); } template <class Slot> void disconnect(Slot slot) { m_sig.disconnect(slot); } protected: friend Friend; boost::signal2::signal<R(Args...)> m_sig; void emit(Args... args) { m_sig.emit(args...); }}; class Demo {public: signal<Demo, void( int )> my_signal; void emit(int val) { my_signal.emit(val); }}; void func( int val ){ cout << "Call func: " << val << endl;} int main(){ Demo demo; demo.my_signal.connect(&func); demo.emit(10); // ok demo.my_signal.emit(10) // Wrong! my_signal.emit is protected! return 0;}
C++ (Qt)DEF_SIGNAL( update, void() )