Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use Qt signal/slot mechanisms over traditional callbacks?

Tags:

c++

callback

qt

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.

like image 553
Etienne Savard Avatar asked Jun 09 '10 15:06

Etienne Savard


People also ask

Are Qt signals and slots thread safe?

It is generally unsafe to provide slots in your QThread subclass, unless you protect the member variables with a mutex. On the other hand, you can safely emit signals from your QThread::run() implementation, because signal emission is thread-safe.

What is a slot how it differs with callback method?

In general: Signals and Slots differs from callbacks by the fact that it decouples the call (Signal) from the Handler (Slot). Which means: you can register your slot to be on a different thread, you can listen to one signal from many slots and change queuing strategy easily.

How Qt signals and slots work?

In Qt, we have an alternative to the callback technique: We use signals and slots. A signal is emitted when a particular event occurs. Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them. A slot is a function that is called in response to a particular signal.

Are Qt signals asynchronous?

So in normal cases, it will be synchronous and blocking, and with queued connections it will be asynchronous and non-blocking.


2 Answers

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.

like image 195
Brian R. Bondy Avatar answered Sep 21 '22 10:09

Brian R. Bondy


You should distinguish between callbacks that are synchronous and used for returning either gradual results or tuples (multiple items, like pair<> but more) and callbacks that are asynchronous loose coupling. Signals/slots should usually be used for the latter. For the former, it can make sense either way depending on whether it is a major function of your application (use signals/slots) or the interface of a non-GUI library that could be more portable (use straight callbacks for plain C/C++ portable code).

Generally, I have concise programming design rules that tend to point to whatever is cleanest and least code / interface complexity. For the tuple return case, I'll often use QMap rather than creating yet another class definition or signal/slot or callback method. This works well for bundling and passing parameters also, especially for things that need to evolve in different parts of the system.

like image 28
sdw Avatar answered Sep 19 '22 10:09

sdw