转自:http://bbs.csdn.net/topics/390147200
A.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#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
|
B.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
#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
|
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#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();
}
|