View Full Version : C callbacks and QT

10th February 2006, 18:28

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.


Chicken Blood Machine
10th February 2006, 19:35
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.

// Code where callback is registered
void MyObject::setup()
// Register C callback
addCallback(&my_callback_function, this /* member data */);

static void my_callback_function(void* data, char* text)
MyObject* ob = static_cast<MyObject*>(data);

// Call the methodfor your object her now

Hope that makes sense

10th February 2006, 20:58
Thank you so much--I literally searched for hours for this answer online with no success. You da man!