抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

self_service_bicycle_parking_device_2440_main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <QCoreApplication>
#include <QObject>
#include <QDebug>
#include "reader.h"
#include "newspaper.h"

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
bool a;
Newspaper newspaper;
Reader reader;
a = QObject::connect(&newspaper, SIGNAL(newPaper(QString )),
&reader, SLOT(receiveNewspaper(QString )));
if(a)
{
newspaper.send("abc");
}else
{
qDebug() << "error."<<endl;
}
return app.exec();
}

reader.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "reader.h"
#include <QDebug>

Reader::Reader(QObject *parent) : QObject(parent)
{

}
Reader::Reader() : QObject()
{

}

void Reader::receiveNewspaper(QString name)
{
qDebug() << "Receives Newspaper: " << name << endl;
}

newspaper.cpp

1
#include "newspaper.h"

newspaper.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
#ifndef NEWSPAPER_H
#define NEWSPAPER_H

#include <QObject>

class Newspaper : public QObject
{
Q_OBJECT
public:
Newspaper(const QString & name)
{
}
Newspaper(): QObject()
{

}
void send(QString m_name)
{
emit newPaper(m_name);
}
signals:
void newPaper(QString name);
};

#endif // NEWSPAPER_H

reader.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef READER_H
#define READER_H

#include <QObject>

class Reader : public QObject
{
Q_OBJECT
public:
Reader();
explicit Reader(QObject *parent);
public slots:
void receiveNewspaper(QString name);

};

#endif // READER_H
  • 注意
    • 1.需要使用qt creator来继承QObject类,不然会报错
    • 2.需要将发送信号函数放在头文件,不然会报信号函数无法链接成功错误

评论