Results 1 to 3 of 3

Thread: C callbacks and QT

  1. #1

    Default C callbacks and QT

    Hello--

    First time posting here, so go easy on me. I'm trying to hook up some legacy C code to Qt and I'm having some problems. Basically, there's a callback function in C that I register for, and that passes me back a char*. I would like to put that information into a textedit box for the user to look at.

    I've been trying to hack it in by calling one of the qt member functions by function pointers but the only problem is that I don't have the correct object to use. (i.e. I can call like a QT member function slot that I made like "Write_To_Display", but it doesn't do anything because I can only pass in a dummy object--the this pointer has no value inside a non member function).

    Is there any easy way to connect C callbacks with a Qt object? Thanks. Btw, I should mention that I'm using Designer and am editting inside the ui.h files.

    Taylor34

  2. #2
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: C callbacks and QT

    When you install a C callback, you can pass some void* member data (usually):
    The trick is to retrieve that pointer in your callback, cast to your object and then call the method you want to invoke on that object.

    Qt Code:
    1. // Code where callback is registered
    2. void MyObject::setup()
    3. {
    4. // Register C callback
    5. addCallback(&my_callback_function, this /* member data */);
    6. }
    7.  
    8. static void my_callback_function(void* data, char* text)
    9. {
    10. MyObject* ob = static_cast<MyObject*>(data);
    11.  
    12. // Call the methodfor your object her now
    13. ob->Write_To_Display(text);
    14. }
    To copy to clipboard, switch view to plain text mode 

    Hope that makes sense
    Last edited by Chicken Blood Machine; 10th February 2006 at 21:40.

  3. #3

    Default Re: C callbacks and QT

    Thank you so much--I literally searched for hours for this answer online with no success. You da man!

    Taylor34

Similar Threads

  1. Qt Plugins and Callbacks
    By abernat in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2009, 23:51
  2. Qt and Callbacks
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 15th November 2006, 16:08

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.