2020-01-09 qt第二天

// widget.cpp
#include "widget.h"
#include 

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    tc = new Teacher(this);
    st = new Student(this);
    //connect(tc,&Teacher::hungry,st,&Student::treat);
    void(Teacher::*teacherSignal)(QString) = &Teacher::hungry;
    void(Student::*studentSlot)(QString) = &Student::treat;

    // 无参数触发信号
    connect(tc,teacherSignal,st,studentSlot);
    classIsOver();

    // 有参数触发信号
    //QPushButton* btn = new QPushButton;
    //btn->setParent(this);
    //btn->setText("下课");
    //connect(btn,&QPushButton::clicked,tc,teacherSignal);
    //connect(tc,teacherSignal,st,studentSlot);
}

void Widget::classIsOver(){
    //emit tc->hungry();
    emit tc->hungry("吃鸡");

}

Widget::~Widget()
{

}
//teacher.cpp
//student.cpp
#include "student.h"
#include 

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

}
void Student::treat(){
    qDebug()<<"请老师吃饭";
}
void Student::treat(QString foodName){
    qDebug()<<"请老师吃饭"<
#include "teacher.h"
#include "student.h"

class Widget : public QWidget
{
    Q_OBJECT

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

    Teacher *tc;
    Student *st;
    //QString foodName;
    void classIsOver();

};

#endif // WIDGET_H


#ifndef TEACHER_H
#define TEACHER_H

#include 

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

signals:
    void hungry();
    void hungry(QString foodName);

public slots:
};

#endif // TEACHER_H

#ifndef STUDENT_H
#define STUDENT_H

#include 

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

signals:

public slots:
    void treat();
    void treat(QString foodName);
};

#endif // STUDENT_H


你可能感兴趣的:(2020-01-09 qt第二天)