tags:

views:

254

answers:

1

A senior developer in my team used traditional C-style callbacks in our Qt application instead of using Qt signal/slot mechanisms.

My first reflex would be to replace his code and use Qt signal/slot instead.

Is there any good reasons to use callbacks in a Qt application/library?

Thanks.

+9  A: 

I think the better approach would be to embrace the framework you are using and use signal/slots.

That being said, if the code in question works, and is not ugly or causing problems, then you may be best to leave it alone.

Consulting the Signal/Slot documentation describes why the Signal/Slot approach is better:

Callbacks have two fundamental flaws: Firstly, they are not type-safe. We can never be certain that the processing function will call the callback with the correct arguments. Secondly, the callback is strongly coupled to the processing function since the processing function must know which callback to call.

Do be aware of the following though:

Compared to callbacks, signals and slots are slightly slower because of the increased flexibility they provide

The speed probably doesn't matter for most cases, but there may be some extreme cases of repeated calling that makes a difference.

Brian R. Bondy
+1 for both embracing the framework and leaving things as they are.
OregonGhost
The docs aren't really accurate. Callbacks are in fact typesafe, unless you use reinterpret_casts. And with reinterpret_casts, signal/slots aren't typesafe either.The processing function certainly doesn't need to know which callback to call. In fact, the most common implementation of callbacks doesn't - the caller passes a function pointer for the callback.
MSalters
@MSalaters: Agree
Brian R. Bondy