当前位置:网站首页>[QT] Implementation of callback function
[QT] Implementation of callback function
2022-08-04 21:32:00 【Cappuccino-jay】
A callback function is a function you write,Let the pre-written system call.You call system functions,is straight.让系统调用你的函数,就是回调.
A 让 B 做事,根据粒度不同,可以理解成 A 函数调用 B 函数,或者 A 类使用 B 类,或者 A 组件使用 B 组件等等.反正就是 A 叫 B 做事.当 B 做这件事情的时候,Not enough information for your own needs,而 A 又有.就需要 A 从外面传进来,或者 B Do what you do and then apply outside.对于 B 来说,A passive access to information,One is to take the initiative to get information.Some people give both ways a term,It's called pressing of information( push),and information pull( pull).
1、callback.h
#ifndef CALLBACK_H
#define CALLBACK_H
/*A 让 B 排序,B 会做排序,But sorting needs to know which is bigger than which, * 这点 B 自己不知道,就需要 A 告诉它.And judging the size itself is an act, * 既然 C A function that cannot be passed a first value in the language,It is designed to pass a function pointer of the second value, * This function pointer is A 传向 B 的信息,Used to describe the behavior of judging size. * 这里本来 A 调用 B 的,结果 B 又调用了 A information to tell it,也就是 callback */
#include <QWidget>
typedef double(*cbFunc)(double,double); //函数指针
class CallBack : public QWidget
{
Q_OBJECT
public:
explicit CallBack(QWidget *parent = nullptr);
void m_getData(cbFunc); //调用回调函数
signals:
public slots:
};
#endif // CALLBACK_H
2、callback.cpp
#pragma execution_character_set("utf-8")
#include "callback.h"
#include <QDebug>
CallBack::CallBack(QWidget *parent) : QWidget(parent)
{
}
void CallBack::m_getData(cbFunc m_cbFunc)
{
double i = m_cbFunc(3.6,5.7);
qDebug() << "The value returned by the callback function: " << i;
}
3、widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "callback.h"
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
CallBack m_callback;
static double m_getPosition(double a, double b);
};
#endif // WIDGET_H
4、widget.cpp
#pragma execution_character_set("utf-8")
#include "widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
//Pass the address of the function to other functions by passing parameters,The function is then called through the function pointer in other functions --回调
m_callback.m_getData(&m_getPosition);
}
Widget::~Widget()
{
}
double Widget::m_getPosition(double a, double b)
{
qDebug() << "The callback function triggers the incoming value: " << a << b;
return a+b;
}
5、运行结果输出

边栏推荐
- DSPE-PEG-Aldehyde, DSPE-PEG-CHO, Phospholipid-Polyethylene Glycol-Aldehyde A hydrophobic 18-carbon phospholipid
- unity2D横版游戏教程9-对话框dialog
- LayaBox---TypeScript---Problems encountered at first contact
- 拼多多开放平台订单信息查询接口【pdd.order.basic.list.get订单基础信息列表查询接口(根据成交时间)】代码对接教程
- Win11如何开启Telnet客户端?
- 驱动点云格式修改带来的效率提升
- Data warehouse (1) What is data warehouse and what are the characteristics of data warehouse
- 【PCBA方案设计】握力计方案
- JWT主动校验Token是否过期
- 模拟对抗之红队免杀开发实践
猜你喜欢
随机推荐
Re24:读论文 IOT-Match Explainable Legal Case Matching via Inverse Optimal Transport-based Rationale Ext
如何一键重装win7系统?重装win7系统详细教程
括号匹配
2022年江苏省大学生电子设计竞赛(TI杯)B题 飞机 省级一等奖记录 “一个摆烂人的独白”
In action: 10 ways to implement delayed tasks, with code!
用Tesseract开发一个你自己的文字识别应用
DSPE-PEG-Aldehyde,DSPE-PEG-CHO,磷脂-聚乙二醇-醛基一种疏水18碳磷脂
Common methods of js's new Function()
命名路由、组件中name的作用
链队
[2022 Hangzhou Electric Multi-School 5 1003 Slipper] Multiple Super Source Points + Shortest Path
三种方式设置特定设备UWP XAML view
强网杯2022——WEB
【SQL之降龙十八掌】01——亢龙有悔:入门10题
中大型商业银行堡垒机升级改造方案!必看!
【2022牛客多校5 A题 Don‘t Starve】DP
信创是什么意思?涉及哪些行业?为什么要发展信创?
bracket matching
jekyll 在博客添加流程图
如何将二叉搜索树转化为一个有序的双向链表(原树上修改)









