General callback on main thread



  • Some time ago I already asked this question, but there was a lot of information and I guess the real question was not really clear. By now I know better what I want:

    Post a(ny) callback on the main thread.

    Can I achieve this somehow? Something like: qApp->postEvent(GeneralCallback) where GeneralCallback could be a global function, the result of for instance std::bind.



  • Hi @Jakob,
    the ad hoc hackish way to implement such system that comes to my mind is:

    • Create an abstract base class AbstractCallback with a virtual 'callback' function
    • Create a class - that inherits AbstractCallback - for each callback function. Add member variables that store the callback arguments and a wrapper function for the actual callback function
    • Post instances of these classes to the event queue
    • Create an event filter that calls the 'callback' functions of those objects


  • @Wieland Thank you for a quick response.

    I was actually thinking along the same directions. Am I correct to assume that such a approach requires me to create a custom event type using QEvent::registerEventType() for the AbstractCallback?

    Additionally, my gut feeling is that creating an event filter on the main event queue (essentially a filter on qApp I presume?) could cause quite some performance loss. Or am I too pessimistic?



  • @Jakob said:

    Am I correct to assume that such a approach requires me to create a custom event type

    Yes.

    [...] could cause quite some performance loss. Or am I too pessimistic?

    Don't know, you'll have to meassure the impact. What I can say is that I used an event filter recently to handle input from a graphics tablet. Processing huge amounts of events in real time was no problem.