CALLBACK macro (QT)

CALLBACK macro (QT)

本文关键字:QT macro CALLBACK      更新时间:2023-10-16

我有一个带有这样签名的函数

int iV_SetSampleCallback(pDLLSetSample pSampleCallbackFunction)  

pDLLSetSample在哪里

typedef int (CALLBACK * pDLLSetSample) (struct SampleStruct rawDataSample);

我想将我的成员函数作为回调传递

int sampleCallbackFunction(struct SampleStruct sample);

当我称它为像

iV_SetSampleCallback(&MainWindow::sampleCallbackFunction);

我收到错误

error: C2664: 'int iV_SetSampleCallback(pDLLSetSample)': cannot convert argument 1 from 'int (__thiscall MainWindow::* )(SampleStruct)' to 'pDLLSetSample'

我不明白为什么会发生这种情况。什么是CALLBACK宏?__thiscall从何而来?我应该如何正确地将回调传递给此函数?

发生错误是因为您尝试传递指向非静态类成员函数的指针sampleCallbackFunction而不是指向常规函数的指针。请注意,非静态类成员函数具有显式this参数。CALLBACK宏通常用于 Windows,通常代表stdcall调用约定。

要修复此错误,您需要

  • 将成员回调函数声明为静态
  • 将 CALLBACK
  • 宏附加到成员函数,以便它具有预期的调用约定(或任何 CALLBACK 扩展的内容)
static int CALLBACK sampleCallbackFunction(struct SampleStruct sample);