信号和槽的拓展
信号是可以连接信号的
信号和槽可以断开 disconnect
一个信号可以连接多个槽函数
多个信号可以连接同一个槽函数
信号和槽函数的个数必须一一对应
信号的参数个数 可以大于槽函数的参数个数 ,反之不可以
QT4的写法如下,但是不推荐,注意到connect()函数的 signal 和 slot 都是接受字符串,一旦出现连接不成功的情况,Qt4是没有编译错误的(因为一切都是字符串,编译期是不检查字符串是否匹配),而是在运行时给出错误。这无疑会增加程序的不稳定性。
//QT4 版本的信号和槽的写法
connect(teacher, SIGNAL(hungry(QString)), student, SLOT(treat(QString)));
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "teacher.h"
#include "student.h"
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
Teacher * teacher;
Student * student;
void classOver();
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include <QPushButton>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
teacher = new Teacher(this);//this表示将Teacher放在Widget的childern()表中,不用管对象释放
student = new Student(this);
//连接2个对象之间的关系
// connect(teacher, &Teacher::hungry, student, &Student::treat);
/*
//有参 信号和槽的链接
//函数指针 执行函数地址
void (Teacher:: * teacherSignal)(QString) = &Teacher::hungry;
void(Student:: * studentSlot )(QString) = &Student::treat;
connect(teacher, teacherSignal, studvoident, studentSlot );
*/
//classOver();//自动调用
//点击按钮才触发
QPushButton * btn = new QPushButton("按钮", this);
//触发无参 信号和槽
void (Teacher:: * teacherSignal)(void) = &Teacher::hungry;
void(Student:: * studentSlot )(void) = &Student::treat;
connect(btn, &QPushButton::clicked, teacher, teacherSignal);
connect(teacher, teacherSignal, student, studentSlot );
//断开信号和槽
// disconnect(teacher, teacherSignal, student, studentSlot);
resize(600, 400);
}
/*
void Widget::classOver()
{
//出发老师类中的信号
//老师的hungry的信号属于自定义的信号,触发自定义信号的关键字 emit
emit teacher->hungry();//触发无参信号
emit teacher->hungry("aaaaaaaaaaaaa");//触发有参信号
}
*/
Widget::~Widget()
{
}
teacher.h
#ifndef TEACHER_H
#define TEACHER_H
#include <QObject>
class Teacher : public QObject
{
Q_OBJECT
public:
explicit Teacher(QObject *parent = 0);
~Teacher();
signals:
//自定义的信号,需要写道signals下面,返回类型必须是void
//没有返回值,不需要实现,只需要声明
//可以有参数,可以重载
void hungry();
void hungry(QString foodName);
public slots:
};
#endif // TEACHER_H
teacher.cpp
#include "teacher.h"
Teacher::Teacher(QObject *parent) : QObject(parent)
{
}
Teacher::~Teacher()
{
}
student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <QObject>
class Student : public QObject
{
Q_OBJECT
public:
explicit Student(QObject *parent = 0);
~Student();
signals:
public slots:
//自定义槽函数
//高版本可以写到上面的public下面,或者全局,最好写在这里防止漏掉了
//槽函数返回值void
//槽函数需要声明也需要实现
//槽函数也可以有参数 可以发生重载
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() <<"Student::treat()=====" ;
}
void Student::treat(QString foodName)
{
//QString 转 char * 先转成QByteArray类型 再转char *
qDebug() <<"Student::treat(QString foodName)=====" << foodName.toUtf8().data();
}
Student::~Student()
{
}
输出
Student::treat()=====