Qtの信号とスロットケース

        Qtの、信号発生器によって一緒に結合されている受信信号における()フォーマットは以下の通りである接続:
        接続(信号発生器、生成された信号、信号受信、処理機能)
        、食べる信号を使用する教師を招待するために学生を与え、スロットメカニズムが実装されています。
        ソフトウェア要件:2つの教師クラスを作成します
        。Studentクラスが終了すると、教師ztは信号送信ます:hungry();
        学生は信号Student stに応答します。信号処理スロット関数:treat()。
次のようになり
ます。//1)メイン関数ヘッダーファイルone.h

#ifndef ONE_H
#define ONE_H

#include <QWidget>
#include "teacher.h"
#include "student.h"

class one : public QWidget
{
    
    
    Q_OBJECT

public:
    one(QWidget *parent = 0);
    ~one();

    Teacher *zt;
    Student *st;

    void classIsOver();

};

#endif // ONE_H

//メイン関数のソースファイルone.cpp

#include "one.h"

//需求:创建2个类 Teacher类,Student类
//下课后, 老师Teacher zt 会发出一个信号 饿了
//学生响应信号Student st 处理信号的槽函数 请客吃饭




one::one(QWidget *parent)
    : QWidget(parent)
{
    
    
    zt = new Teacher(this);
    st = new Student(this);

    //连接 老师和学生
    connect(zt,&Teacher::hungry,st,&Student::treat );
    classIsOver();
}

one::~one()
{
    
    

}

void one::classIsOver()
{
    
    
    //触发老师饿了的信号
    emit zt->hungry();
}

// 2)Student.hヘッダーファイル

#ifndef STUDENT_H
#define STUDENT_H

#include <QObject>

class Student : public QObject
{
    
    
    Q_OBJECT
public:
    explicit Student(QObject *parent = 0);

signals:

public slots:
    //自定义槽函数
    //槽函数和信号匹配
    void treat();

};

#endif // STUDENT_H

//学生のソースファイルStudent.cpp

#include "student.h"
#include <QDebug>

Student::Student(QObject *parent) :
    QObject(parent)
{
    
    
}

void Student::treat()
{
    
    
    qDebug()<<"请老师吃饭";
}

// 3)教師クラスヘッダーファイルStudent.h

#ifndef TEACHER_H
#define TEACHER_H

#include <QObject>

class Teacher : public QObject
{
    
    
    Q_OBJECT
public:
    explicit Teacher(QObject *parent = 0);

signals:
    //自定义的信号,需要写到signals下
    //返回类型必须为空,信号只需要声明,不需要实现
    //信号可以有参数,可以重载
    void hungry();

public slots:

};

#endif // TEACHER_H

//教師のソースファイルStudent.cpp

#include "teacher.h"

Teacher::Teacher(QObject *parent) :
    QObject(parent)
{
    
    
}

        効果は次のとおりです。
ここに写真の説明を挿入

おすすめ

転載: blog.csdn.net/sanqima/article/details/103844124