Qthread类中的回调功能
Callback function in QThread Class
我有一个基于qthread的类,基本上是GUI线程。在此线程中,我使用的另一个具有此函数类型定义的类:
void SomFunc(const std::function<void (int, std::string, int)> &data)
我想在我的课堂中创建一个回调函数,例如myThread ::回调并在上面调用函数,并将我的myThread ::回调函数作为实际回调函数传递。无论我尝试什么,我都会想念一些东西,我真的很困惑的是std ::功能函数,需要帮助。我如何定义一个可以将其作为参数传递给SomFunc
的函数,并在我的MyThread类上下文中获取正确的回调
如果我只是创建一个void函数,这就是我得到的:
error: reference to type 'const std::function<void (int, std::string, int)>' (aka 'const function<void (int, basic_string<char>, int)>') could not bind to an rvalue of type 'void (MyClass::*)(int, std::string, int)'
您可以按以下方式执行:
#include <iostream>
#include <string>
void f(int a, std::string b, int c)
{
std::cout << a << " -- " << b << " -- " << c << std::endl;
}
void someFunc(void (inner)(int, std::string, int), int a, std::string b, int c)
{
inner(a, b, c);
}
int main()
{
int a = 5;
std::string b("text");
int c = 10;
someFunc(f, a, b, c);
return 0;
}
也可以明确传递指针或参考:
void someFunc(void (*inner)(int, std::string, int), int a, std::string b, int c)
// OR
void someFunc(void (&inner)(int, std::string, int), int a, std::string b, int c)
如果使用指针语法,则可以通过:
替换呼叫someFunc(&f, a, b, c);
但是,无论如何,编译器都会通过指针静静地替换您的语法选择,因此您无需明确使用C 中的指针语法。
希望它能有所帮助。
相关文章:
- 架构决策:返回std::future还是提供回调
- 正在为Xtensa simcall函数编写回调函数
- 可以使用仅功能成员属性进行回调界面
- 在Cassandra中使用回调功能
- Qthread类中的回调功能
- 如何使用C lambda将成员功能指针转换为普通功能指针,以用作回调
- 参数化回调功能
- libpcap C/C 回调功能
- C 将功能模板作为参数作为回调
- 在C 中的类中实现回调功能
- 将C 回调功能转换为Delphi
- 通过功能指针通过DLL进行回调功能,运行时检查失败#0错误
- 带有功能指针的C 回调
- pybind11基本回调,不兼容的功能签名错误
- 通过回调功能,打破一个循环的最佳方法是什么?
- Cython:具有自定义参数类型的STD ::功能回调
- 非成员功能指针作为API中的回调对成员函数
- JavaScript和C 回调功能具有参数
- c++FLTK(更多具有相同回调功能的按钮)
- 如何使一个回调全局,所以我可以使用它的其他功能