当前位置:网站首页>[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、运行结果输出
边栏推荐
猜你喜欢
SPSS-System Clustering Software Practice
Android 面试——如何写一个又好又快的日志库?
[2022 Hangzhou Electric Power Multi-School 5 1012 Questions Buy Figurines] Application of STL
3、IO流之字节流和字符流
数电快速入门(三)(卡诺图化简法的介绍)
3. Byte stream and character stream of IO stream
OD-Model【6】:YOLOv2
数电快速入门(二)(复合逻辑运算和逻辑代数的基本定律的介绍)
SPSS-unary regression practice
proe和creo的区别有哪些
随机推荐
PRIMAL: Pathfinding via Reinforcement and Imitation Multi-Agent Learning 代码解析
【2022牛客多校5 A题 Don‘t Starve】DP
【Programming Ideas】
热力学相关的两个定律
【线性代数02】AX=b的2种解释和矩阵乘法的5种视角
2、字符集-编码-解码
实战:10 种实现延迟任务的方法,附代码!
mdk5.14 cannot be burned
Spss-系统聚类软件实操
js的new Function()常用方法
[2022 Nioke Duo School 5 A Question Don't Starve] DP
LayaBox---知识点
数电快速入门(四)(组合逻辑电路的分析以及设计的介绍)
括号匹配
milvus配置相关
Three ways to set a specific device UWP XAML view
88. (the home of cesium) cesium polymerization figure
【手把手教你使用STM32HAL库的串口空闲中断】
强网杯2022——WEB
【PCBA program design】Grip dynamometer program