创建两个类student和teacher,父类都为QObject
在teacher中自定义Signal,hungry
- //自定义信号,写道signals下
- signals:
- //返回值void
- //只需要声明
- //可以有参数,可以重载
- void hungry();
-
- void hungry(QString FoodName);
在student中自定义Slot,treat
- //自定义槽函数,写到public slots Qt5.0版本以上可以写成全局函数或者public作用域下或者lambda表达式
- public slots:
- //返回值void
- //需要声明和实现
- //可以有参数,可以重载
- void treat();
-
- void treat(QString FoodName);
当teacher对象发出hungry信号,调用treat槽函数
connect(tc, &teacher::hungry, st, &student::treat);
当遇到重载的槽函数时,需用函数指针指向对应的函数
- //连接信号槽
-
- //连接有参数信号和槽
- //当信号和槽发生重载的时候,函数指针可以指向函数地址,用函数指针明确对应的函数
- void(teacher::*teacherSignal)(QString) = &teacher::hungry;
- void(student::*studentSlot)(QString) = &student::treat;
- void(student::*studentSlotEmpty)() = &student::treat;
- connect(tc, teacherSignal, st, studentSlot);
- connect(btn1, &QPushButton::clicked, st, studentSlotEmpty);
Qstring转换为char类型的方法
- //QString转char* 通过.toUtf8转为QByteArrary类型通过.data()转为char*
- qDebug() << "请老师吃" << FoodName.toUtf8().data();
student.h
- #ifndef STUDENT_H
- #define STUDENT_H
-
- #include
- #include
-
- class student : public QObject
- {
- Q_OBJECT
- public:
- explicit student(QObject *parent = nullptr);
-
- signals:
-
- //自定义槽函数,写到public slots Qt5.0版本以上可以写成全局函数或者public作用域下或者lambda表达式
- public slots:
- //返回值void
- //需要声明和实现
- //可以有参数,可以重载
- void treat();
-
- void treat(QString FoodName);
-
- void treat(QString FoodName, QString SecondFoodName);
- };
-
- #endif // STUDENT_H
teacher.h
- #ifndef TEACHER_H
- #define TEACHER_H
-
- #include
- #include
-
- class teacher : public QObject
- {
- Q_OBJECT
- public:
- explicit teacher(QObject *parent = nullptr);
-
- //自定义信号,写道signals下
- signals:
- //返回值void
- //只需要声明
- //可以有参数,可以重载
- void hungry();
-
- void hungry(QString FoodName);
- };
-
-
-
- #endif // TEACHER_H
widget.h
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include
- #include
- #include
- #include "mypushbutton.h"
- #include "teacher.h"
- #include "student.h"
-
- QT_BEGIN_NAMESPACE
- namespace Ui { class Widget; }
- QT_END_NAMESPACE
-
- class Widget : public QWidget
- {
- Q_OBJECT
-
- public:
- Widget(QWidget *parent = nullptr);
- ~Widget();
- teacher* tc;
- student* st;
- QPushButton* btn1;
- QPushButton* btn2;
- QPushButton* btn3;
- QPushButton* btn4;
- void display(QTextEdit *text1);
- void classIsOver();
- private:
- Ui::Widget *ui;
- };
- #endif // WIDGET_H
widget.cpp
- #include "widget.h"
- #include "ui_widget.h"
-
-
- Widget::Widget(QWidget *parent)
- : QWidget(parent)
- , ui(new Ui::Widget)
- {
- ui->setupUi(this);
- //new时括号内加入this,会将对象放入对象树,会自动调用析构函数
- this->tc = new teacher(this);
- this->st = new student(this);
- this->btn1 = new QPushButton;
- this->btn2 = new QPushButton;
- this->btn3 = new QPushButton("close", this);
- this->btn4 = new QPushButton("吃两个菜", this);
- btn1->setParent(this);
- btn1->setText("下课");
-
- btn2->setParent(this);
- btn2->setText("display");
- btn2->move(0,200);
-
- btn3->move(0,300);
- btn4->move(0,250);
-
-
- //连接信号槽
-
- //连接有参数信号和槽
- //当信号和槽发生重载的时候,函数指针可以指向函数地址,用函数指针明确对应的函数
- void(teacher::*teacherSignal)(QString) = &teacher::hungry;
- void(student::*studentSlot)(QString) = &student::treat;
- connect(tc, teacherSignal, st, studentSlot);
-
-
- //信号可以连接信号
- void(teacher::*teacherSignalEmpty)() = &teacher::hungry;
- void(student::*studentSlotEmpty)() = &student::treat;
- connect(tc, teacherSignalEmpty, st, studentSlotEmpty);
- connect(btn1, &QPushButton::clicked, tc, teacherSignalEmpty);
- //信号可以断开连接
- //disconnect(btn1, &QPushButton::clicked, tc, teacherSignalEmpty);
- //一个信号可以绑定多个槽函数
- //多个信号可以绑定同一个槽函数
- connect(btn2, &QPushButton::clicked, tc, teacherSignalEmpty);
- //槽函数的参数和信号的参数类型必须一一对应
- //信号的参数个数可以多余槽函数的参数个数,反之不可以
-
- //链接槽函数 注:信号的参数要大于等于槽函数的参数,
- //点击事件无参,但是槽函数需要传递ui参数
- //故槽函数采用Qt5之后支持的lambda表达式进行传参,常用=传递值,而不是&,因为当槽和信号连接时,当前控件会进入锁的状态,而=传递是创造新的值覆盖,不会过多影响效率,加上mutable关键字,可以修改按值传递的拷贝
- //新建一个textedit
- QTextEdit *text1 = new QTextEdit(this);
- //设置大小为200*100并放置在(300,100)处
- text1->resize(200, 100);
- text1->move(300, 100);
-
- //重置窗体大小为600*400
- this->resize(600, 400);
- connect(btn2, &QPushButton::clicked, text1, [=](){display(text1);});
- //Qt4版本信号和槽信号写法(不推荐)
- //利用Qt4版本连接有参信号和槽
- //优势参数直观
- //劣势参数类型不做匹配检测
- //connect(tc, SIGNAL(hungry(QString), st, SLOT(treat(QString)));
-
- //点击按钮,关闭窗口
- connect(btn3, &QPushButton::clicked, this, [=](){this->close();});
-
- connect(btn4, &QPushButton::clicked, this, [=](){this->st->treat("宫保鸡丁", "鱼香肉丝");});
-
- classIsOver();
- }
-
- void Widget::display(QTextEdit *text1)
- {
- text1->setText("I Love you more than I can say!");
- text1->append("我爱你在心口难开");
- }
-
- void Widget::classIsOver()
- {
- //触发自定义信号,emit是Qt关键字,使用:在A中对B发出信号
- emit this->tc->hungry("宫保鸡丁");
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
-