PDA

View Full Version : How to emit signal from static callback function



blizniak83
12th April 2012, 20:55
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.


// 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

Lykurg
12th April 2012, 22:42
Does it work if you call a function of "static_cast<MyCustomClass*>" which emits the signal?

ChrisW67
13th April 2012, 02:04
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).

blizniak83
14th April 2012, 08:56
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.