class A : QObject{Q_OBJECTpublic: void Print(); A(){ a1 = rand();}; ModifiA1(int _a1){ a1 = _a1; emit SignalS(a1);};signal: SignalS(int Data);public slots: SlotS(int Data){ a2 = Data;};private: int a1,a2;};class B : QObject{Q_OBJECTpublic: B(){ a1 = new A; a2 = new A; connect(a1, SIGNAL(SignalS(int)), a2, SLOT(SlotS(int)); connect(a2, SIGNAL(SignalS(int)), a1, SLOT(SlotS(int));}; ModifiA1(int a){ a1.ModifiA1(a); } ModifiA2(int a){ a2.ModifiA1(a); }private: A *a1,*a2;}
#ifndef QTESTCLASS_H#define QTESTCLASS_H#include <QObject>class QTestClass : public QObject{ Q_OBJECTpublic: explicit QTestClass(QObject *parent = 0); void CallSignl(int a);protected: virtual void connectNotify(const char *signal);signals: void TestSignal(int a);public slots: void TestSlot(int a);};#endif // QTESTCLASS_H
#include "QTestClass.h"#include <iostream>#include <QLatin1String>using namespace std;QTestClass::QTestClass(QObject *parent) : QObject(parent){}void QTestClass::CallSignl(int a){ emit TestSignal(a);}void QTestClass::connectNotify(const char *signal){ if (QLatin1String(signal) == SIGNAL(TestSignal(int))) { cout<<"Сигнал был соединен со слотом"<<endl; }}void QTestClass::TestSlot(int a){ cout<<a<<endl;}
#include <QApplication>#include <QObject>#include "QTestClass.h"int main(int argc, char **argv){ QApplication app(argc, argv); QTestClass *a = new QTestClass; QObject::connect(a, SIGNAL(TestSignal(int)), a, SLOT(TestSlot(int))); a->CallSignl(10); QObject::connect(a, SIGNAL(TestSignal(int)), a, SLOT(TestSlot(int))); a->CallSignl(15); return app.exec();}