- 自定义信号
1.写到signal下
2.返回void
3.需要声明,不需要实现
4.可以有参数,可以重载
- 自定义槽函数
1.返回void
2.需要声明,也需要实现
3.可以有参数,可以重载
4.写到pulic slot下或者pulic或者全局函数
- 触发自定义的信号
1.emit 自定义信号
- 案例:下课后,老师触发饿了信号,学生响应信号,请客吃饭。
#ifndef TEACHER_H
#define TEACHER_H
#include <QObject>
class Teacher : public QObject
{
Q_OBJECT
public:
explicit Teacher(QObject *parent = nullptr);
signals:
void hungry();
};
#endif
#include "teacher.h"
Teacher::Teacher(QObject *parent)
: QObject{parent}
{}
#ifndef STUDENT_H
#define STUDENT_H
#include <QObject>
class Student : public QObject
{
Q_OBJECT
public:
explicit Student(QObject *parent = nullptr);
signals:
public slots:
void treat();
};
#endif
#include "student.h"
#include<QDebug>
Student::Student(QObject *parent)
: QObject{parent}
{}
void Student::treat()
{
qDebug()<<"老师饿了,学生请客吃饭";
}
QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void classOver();
private:
Ui::Widget *ui;
Teacher*t;
Student*s;
};
#endif
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->t=new Teacher(this);
this->s=new Student(this);
connect(t,&Teacher::hungry,s,&Student::treat);
classOver();
}
void Widget::classOver()
{
qDebug()<<"下课了!";
emit t->hungry();
}
Widget::~Widget()
{
delete ui;
}

【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性