16,216
社区成员
发帖
与我相关
我的任务
分享
#ifndef A_H
#define A_H
#include <QObject>
#include <QDebug>
class A : public QObject
{
Q_OBJECT
public:
explicit A(QObject *parent = 0) :
QObject(parent)
{
}
void sendSignalToB()
{
emit toB();
}
signals:
void toB();
public slots:
void fromB()
{
qDebug() << "i am A ,Rcv from B.";
}
};
#endif // A_H
#ifndef B_H
#define B_H
#include <QObject>
#include <QDebug>
class B : public QObject
{
Q_OBJECT
public:
explicit B(QObject *parent = 0) :
QObject(parent)
{
}
void sendSignalToA()
{
emit toA();
}
signals:
void toA();
public slots:
void fromA()
{
qDebug() << "i am B ,Rcv from A.";
}
};
#endif // B_H
#include <QtCore/QCoreApplication>
#include "a.h"
#include "b.h"
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
A *a = new A;
B *b = new B;
app.connect(a, SIGNAL(toB()), b, SLOT(fromA()));
app.connect(b, SIGNAL(toA()), a, SLOT(fromB()));
a->sendSignalToB();
b->sendSignalToA();
return app.exec();
}