How to emit signal from static callback function
Hi,
Is there a way of emitting a signal from static callback function?
Below is the example I found online which apparently should be fine.
Code:
// MyCustomClass.h
...
protected:
static void XN_CALLBACK_TYPE OnPointUpdate(const XnVHandPointContext* pContext, void* cxt);
signals:
mySignal(QVector3D v);
...
//=======================
//=======================
// MyCustomClass.cpp
...
void XN_CALLBACK_TYPE MyCustomClass::OnPointUpdate(const XnVHandPointContext* pContext, void* cxt)
{
QVector3D v;
v.setX(pContext->ptPosition.X);
v.setY(pContext->ptPosition.Y);
v.setZ(pContext->ptPosition.Z);
// this is where the problems start...
emit static_cast<MyCustomClass*>(cxt)->mySignal(v);
}
...
When I tried to debug my program to see where it fails, this is what I got:
"Unhandled exception at 0x60893f3a (QtCored4.dll) in test.exe: 0xC0000005: Access violation reading location 0x00000004."
... and that happened in QScopedPointer.h at line 135.
Currently I'm using a global variable to access my 'QVector3D v'.
Is there a better way of overcoming this problem?
Thank you.
Krzysztof
Re: How to emit signal from static callback function
Does it work if you call a function of "static_cast<MyCustomClass*>" which emits the signal?
Re: How to emit signal from static callback function
The error message is because the value of "cxt" is not valid, i.e. is not a valid pointer to instance of MyCustomClass. Chances are good that it is 0 (NULL).
Re: How to emit signal from static callback function
Thanks Chris,
Not valid pointer, that was the problem and yes it was NULL.
It was just the case of changing NULL to this, and now works fine.