Results 1 to 5 of 5

Thread: Qt slot as a callback function to external library?

  1. #1
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Qt slot as a callback function to external library?

    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.

  2. #2
    Join Date
    Dec 2007
    Posts
    27
    Thanks
    1
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt slot as a callback function to external library?

    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?

  3. #3
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt slot as a callback function to external library?

    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.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Qt slot as a callback function to external library?

    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.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt slot as a callback function to external library?

    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?

Similar Threads

  1. Qt classes in a callback function
    By Luc4 in forum Qt Programming
    Replies: 1
    Last Post: 13th May 2010, 13:47
  2. Interoperability of C Callback function and Qt
    By Vogel Ubrhar in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2010, 16:02
  3. structs, pointer, static callback function
    By vonCZ in forum General Programming
    Replies: 3
    Last Post: 20th June 2008, 12:53
  4. how to define the callback function in QThread?
    By cxl2253 in forum Qt Programming
    Replies: 6
    Last Post: 30th March 2007, 10:59
  5. (Callback API) and (Qt - SIGNAL/SLOT)
    By pytro in forum Qt Programming
    Replies: 19
    Last Post: 13th March 2007, 20:14

Tags for this Thread

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.