Qt5 emit总结

//Qt5 emit的使用//通过emit我们可以限定发送信号的时机//传递信号和槽的参数//及改变信号的状态
//通过网上的说明自己添加了自己总结的特性要源文件可q我
#include <QCoreApplication>
#include"newspaper.h"
#include"reader.h"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Newspaper newspaper("newspaper  A");
    Reader reader;
    QObject::connect(&newspaper,&Newspaper::newPaper,&reader,&Reader::receiveNewspaper);
    newspaper.send();
    //改变信号状态
    newspaper.set("newspaper A2");
    newspaper.send();
    return a.exec();
}
下面附上两个头文件
//newspaper.h
#ifndef NEWSPAPER_H
#define NEWSPAPER_H
#include<QObject>
class Newspaper:public QObject
      {
    Q_OBJECT
public:
    Newspaper(const QString &name):m_name(name)
    {
    }
    void send()
    {
        emit newPaper(m_name);
    }
    //基于豆子的添加代码
    void set(const QString &newname)
    {
        m_name = newname;
    }
signals:
    void newPaper(const QString& name);
private:
    QString m_name;
};
#endif // NEWSPAPER_H
#ifndef READER_H
//reader.h
#define READER_H
#include<QObject>
#include<QDebug>
class Reader:public QObject
{
    Q_OBJECT
public:
    Reader(){}
    void receiveNewspaper(const QString &name)
    {
        qDebug() << "Receive NewsPaper" << name;
    }
};
#endif // READER_H

©️2020 CSDN 皮肤主题: 我行我“速” 设计师:Amelia_0503 返回首页