Results 1 to 4 of 4

Thread: How to emit signal from static callback function

  1. #1
    Join Date
    Sep 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

    Qt Code:
    1. // MyCustomClass.h
    2. ...
    3. protected:
    4. static void XN_CALLBACK_TYPE OnPointUpdate(const XnVHandPointContext* pContext, void* cxt);
    5.  
    6. signals:
    7. mySignal(QVector3D v);
    8. ...
    9.  
    10. //=======================
    11. //=======================
    12.  
    13. // MyCustomClass.cpp
    14. ...
    15. void XN_CALLBACK_TYPE MyCustomClass::OnPointUpdate(const XnVHandPointContext* pContext, void* cxt)
    16. {
    17. QVector3D v;
    18. v.setX(pContext->ptPosition.X);
    19. v.setY(pContext->ptPosition.Y);
    20. v.setZ(pContext->ptPosition.Z);
    21.  
    22. // this is where the problems start...
    23.  
    24. emit static_cast<MyCustomClass*>(cxt)->mySignal(v);
    25.  
    26. }
    27. ...
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by blizniak83; 12th April 2012 at 20:56. Reason: updated contents

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default 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?

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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).

  4. #4
    Join Date
    Sep 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

Similar Threads

  1. how to emit signal in a static function ?
    By cxl2253 in forum Qt Programming
    Replies: 32
    Last Post: 7th July 2016, 21:36
  2. Can you send a signal from a static callback function?
    By Eos Pengwern in forum Qt Programming
    Replies: 2
    Last Post: 12th August 2010, 13:47
  3. emit a signal from inside a callback routine
    By franco.amato in forum Qt Programming
    Replies: 20
    Last Post: 21st January 2010, 18:05
  4. structs, pointer, static callback function
    By vonCZ in forum General Programming
    Replies: 3
    Last Post: 20th June 2008, 12:53
  5. QString static callback function from CURL
    By tpf80 in forum Qt Programming
    Replies: 12
    Last Post: 16th May 2007, 20:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.