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:
//自定義信號 寫到slots下,低版本可以卸載public下
//返回值是void 需要聲明 也需要實現(xiàn)
//可以有參數(shù)悯仙,可以重載
void treat();
void treat(QString FoodName);
};
#endif // STUDENT_H
student.cpp :
#include "student.h"
#include<QDebug>
Student::Student(QObject *parent) :
QObject(parent)
{
}
void Student::treat()
{
qDebug()<<"請老師吃飯";
}
void Student::treat(QString FoodName)
{
qDebug()<<"請老師吃:"<<FoodName.toUtf8().data();
}
teacher.h :
#ifndef TEACHER_H
#define TEACHER_H
#include <QObject>
class Teacher : public QObject
{
Q_OBJECT
public:
explicit Teacher(QObject *parent = 0);
signals:
//自定義信號 寫到signal下
//返回值是void 只需要聲明 不需要實現(xiàn)
//可以有參數(shù)民傻,可以重載
void hungry();
void hungry(QString FoodName);
public slots:
};
#endif // TEACHER_H
teacher.cpp:
#include "teacher.h"
Teacher::Teacher(QObject *parent) :
QObject(parent)
{
}
widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include"teacher.h"
#include"student.h"
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
Teacher * zt;
Student * st;
void ClassOver();
};
#endif // WIDGET_H
widget.cpp :
#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>
//Teacher 類 老師類
//Student 類 學生類
// 下課了 老師發(fā)出一個餓了的信號 學生請吃飯
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//創(chuàng)建老師對象
this ->zt = new Teacher(this);
//創(chuàng)建學生對象
this ->st = new Student(this);
// //老師餓了,學生請吃飯連接
// //發(fā)個信號
// connect(zt,&Teacher::hungry,st,&Student::treat);
// ClassOver();
//重載時 連接信號和槽 需要定義一個指針
void(Teacher:: *teacherSingal)(QString)= &Teacher::hungry;
void(Student:: *studentSlot)(QString) = &Student::treat;
connect(zt,teacherSingal,st,studentSlot);
//ClassOver();
//點擊一個下課按鈕
QPushButton *btn = new QPushButton("下課",this);
resize(600,400);
//點擊按鈕觸發(fā)下課
//connect(btn,&QPushButton::clicked,this,&Widget::ClassOver);
//無參數(shù)信號和槽連接
void(Teacher:: *teacherSingal2)(void)= &Teacher::hungry;
void(Student:: *studentSlot2)(void) = &Student::treat;
connect(zt,teacherSingal2,st,studentSlot2);
//信號連接信號
connect(btn,&QPushButton::clicked,zt,teacherSingal2);
//斷開信號
//disconnect(zt,teacherSingal2,st,studentSlot2);
//拓展
//1、信號可以連接信號
//2、一個信號可以連接多個槽函數(shù)
//3、多個信號也可以連接一個槽函數(shù)
//4澈蟆、信號和槽函數(shù)的參數(shù) 一一對應(yīng)的
//5趟径、信號的參數(shù)個數(shù)可以多余槽函數(shù)參數(shù)的個數(shù)瘪吏,但是有的參數(shù)類型要對應(yīng)
//版本4的信號和槽連接函數(shù)
//connect(zt,SIGNAL(hungry()),st,SLOT(treat()));
//4版本優(yōu)點,參數(shù)直觀蜗巧,缺點:編譯器對類型不做檢測(前后參數(shù)不一樣不做檢測)
// //Lambda表達式知識 匿名函數(shù)
//
// [=](){
// btn->setText("aa");
// }; //沒()只是起到聲明作用掌眠;
// [=](){
// btn->setText("aa");
// }(); //有()才會執(zhí)行
//mutable關(guān)鍵字永續(xù)修飾值傳遞的變量,修改的時拷貝,而不是值,例子沒寫
//利用lambda表達式實現(xiàn)點擊按鈕 關(guān)閉窗口
QPushButton *btn2 = new QPushButton(this);
btn2->setText("關(guān)閉窗口");
btn2->move(100,0);
//connect(btn2,&QPushButton::clicked,this,&Widget::close);
connect(btn2,&QPushButton::clicked,this,[=](){ //若用lambda表達式連接 若第三個參數(shù)是this可以省略
//this->close();
emit zt->hungry("宮保雞丁");
btn2->setText("bye!");
});
}
Widget::~Widget()
{
delete ui;
}
void Widget::ClassOver()
{
// emit zt->hungry();
emit zt->hungry("宮保雞丁");
}