当前位置:网站首页>[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、运行结果输出
边栏推荐
猜你喜欢
【CC3200AI 实验教程 1】疯壳·AI语音人脸识别(会议记录仪/人脸打卡机)-开发环境搭建
DSPE-PEG-Aldehyde, DSPE-PEG-CHO, Phospholipid-Polyethylene Glycol-Aldehyde A hydrophobic 18-carbon phospholipid
JWT主动校验Token是否过期
OD-Model【6】:YOLOv2
【PCBA program design】Grip dynamometer program
【手把手教你使用STM32HAL库的串口空闲中断】
How to understand the crawler's Scrapy framework in the simplest and most popular way?
PRIMAL: Pathfinding via Reinforcement and Imitation Multi-Agent Learning 代码解析
【线性代数02】AX=b的2种解释和矩阵乘法的5种视角
[2022 Nioke Duo School 5 A Question Don't Starve] DP
随机推荐
PyTorch Geometric (PyG) 安装教程
【编程思想】
OD-Model【6】:YOLOv2
mdk5.14 cannot be burned
deepstream多相机显示布局
数电快速入门(三)(卡诺图化简法的介绍)
PRIMAL: Pathfinding via Reinforcement and Imitation Multi-Agent Learning Code Analysis
命名路由、组件中name的作用
In action: 10 ways to implement delayed tasks, with code!
js的new Function()常用方法
dotnet 删除只读文件
js数据类型、节流/防抖、点击事件委派优化、过渡动画
经验分享|盘点企业进行知识管理时的困惑类型
如何最简单、通俗地理解爬虫的Scrapy框架?
LayaBox---TypeScript---structure
数据仓库(1)什么是数据仓库,数仓有什么特点
boostrap多选PID查找端口 window
laravel whereDoesntHave
知识分享|如何设计有效的帮助中心,不妨来看看以下几点
AXI interface application of Zynq Fpga image processing - the use of axi_lite interface