class A: public QObject{ Q_OBJECT ........ signals: void a(); ........};class B: public QObject{ Q_OBJECT A *aB; ........ public slots: void onAB(); ........};B::B(QObject *parent) : QObject(parent) , aB(new A(this)){ ........ connect(aB, SIGNAL(a()), this, SLOT(onAB())); ........}class C: public QObject{ Q_OBJECT A *aC; ........ public slots: void onAC(); ........};C::C(QObject *parent) : QObject(parent) , aC(new A(this)){ ........ connect(aC, SIGNAL(a()), this, SLOT(onAC())); ........}
QT += coreQT -= guiTARGET = testSignalsCONFIG += consoleCONFIG -= app_bundleTEMPLATE = appSOURCES += main.cpp \ a.cpp \ b.cpp \ c.cppHEADERS += \ a.h \ b.h \ c.h
#ifndef A_H#define A_H#include <QObject>#include <QTimer>class A : public QObject{ Q_OBJECT QTimer *time;public: explicit A(QObject *parent = 0); signals: void a();public slots: void aA(); };#endif // A_H
#include "a.h"#include <QDebug>A::A(QObject *parent) : QObject(parent) , time(new QTimer(this)){ time->setInterval(22500); time->start(); connect(time, SIGNAL(timeout()), this, SLOT(aA())); connect(time, SIGNAL(timeout()), this, SIGNAL(a()));}void A::aA(){ qDebug() << "void A::aA(): signal is emited";}
#ifndef B_H#define B_H#include <QObject>#include "a.h"#include <QTimer>class B : public QObject{ Q_OBJECT A *aB; QTimer *time;public: explicit B(QObject *parent = 0); signals: public slots: void onAB();};#endif // B_H
#include "b.h"#include <QDebug>B::B(QObject *parent) : QObject(parent) , aB(new A(this)) , time(new QTimer(this)){ aB->setObjectName("aB"); time->setInterval(5000); time->start(); connect(aB, SIGNAL(a()), this, SLOT(onAB())); connect(time, SIGNAL(timeout()), this, SLOT(onAB()));}void B::onAB(){ qDebug() << "B::onAB()" << QObject::sender();}
#ifndef C_H#define C_H#include <QObject>#include "a.h"class C : public QObject{ Q_OBJECT A *aC;public: explicit C(QObject *parent = 0); signals: public slots: void onAC();};#endif // C_H
#include "c.h"#include "a.h"#include <QDebug>C::C(QObject *parent) : QObject(parent) , aC(new A(this)){ aC->setObjectName("aC"); connect(aC, SIGNAL(a()), this, SLOT(onAC()));}void C::onAC(){ qDebug() << "C::onAC()" << QObject::sender();}
#include <QCoreApplication>#include "b.h"#include "c.h"int main(int argc, char *argv[]){ QCoreApplication app(argc, argv); B b; C c; return app.exec();}
#include <QCoreApplication>#include "b.h"#include "c.h"#include "a.h"int main(int argc, char *argv[]){ QCoreApplication app(argc, argv); A a; B b; C c; QObject::connect(&a, SIGNAL(a()), &b, SLOT(onAB())); QObject::connect(&a, SIGNAL(a()), &c, SLOT(onAC())); return app.exec();}
B::onAB() QTimer(0x135a230) B::onAB() QTimer(0x135a230) B::onAB() QTimer(0x135a230) B::onAB() QTimer(0x135a230) void A::aA(): signal is emited B::onAB() A(0x134fd30, name = "aB") void A::aA(): signal is emited C::onAC() A(0x1362920, name = "aC") B::onAB() QTimer(0x135a230)
B::onAB() QTimer(0x14f5930) B::onAB() QTimer(0x14f5930) B::onAB() QTimer(0x14f5930) B::onAB() QTimer(0x14f5930) void A::aA(): signal is emited B::onAB() A(0x7fffb337bf20) C::onAC() A(0x7fffb337bf20) void A::aA(): signal is emited B::onAB() A(0x14ed1d0, name = "aB") void A::aA(): signal is emited C::onAC() A(0x14f5df0, name = "aC") B::onAB() QTimer(0x14f5930)