PDA

View Full Version : Qt slot as a callback function to external library?



Yes
27th November 2011, 18:06
Hi, I'm writing a library which I have currently connected to a Qt application that works as a GUI for the library. The library needs to be provided with a callback function and also a parameter in the form of a void pointer. When the library wants to provide the user with information, it calls the callback function and sends it two parameters, 1: The pointer the library was provided with along with the callback function, and 2: The string to be displayed to the user. The idea with the void pointer is that it can point to a class object in order to make it possible to access non-static members of that class, which is not possible otherwise.

Now to my question: Is it possible to create a slot with Qt in some way, in order to make this callback type safe? After all, I don't want to have to receive a void pointer which I may forget what it is for, convert to the wrong kind of pointer and use for the wrong purpose. This non-type safe programming would not either generate a warning (I assume). Instead, I want to wrap this all up in some way, in order to prevent getting back a void pointer. Maybe I could instead get back a pointer to the kind of object I really want to access.

yakin
27th November 2011, 19:49
If a data pointer is void, the pointer has no typeinfo. There is no way to test if the pointer has a specific type. (no dynamic_cast or qobject_cast will work). If you know the original type, you can use reinterpret_cast the pointer back to a typed pointer.
Why not to use pointer to your class instead of void pointer?

Yes
27th November 2011, 20:30
Because it should be possible to plug the library into other user interfaces too. If I use a pointer to the class instead of a void pointer I limit myself to only using the library with Qt applications.

Santosh Reddy
28th November 2011, 02:29
If a data pointer is void, the pointer has no typeinfo. There is no way to test if the pointer has a specific type. (no dynamic_cast or qobject_cast will work).
dynamic_cast and qobject_cast will work, irrespective of how the pointer is passed (void pointer / class pointer) it will contain the typeinfo which was used to create the object.

Yes
28th November 2011, 02:54
What do you mean by that it will contain the typeinfo used to create the object? Since it's just a void pointer, will it contain anything more than just a memory address?