The daily ramblings …

QT : Signals and Slots Mechanism

Posted in QT by akamal on January 16, 2007

One great feature installed inside Qt is the signal and slot mechanism. It’s specially developed for event driven software approach, where no single use case dictates the flow of the software. This feature indeed is central to the whole of Qt concept, which probably made it different to other frameworks.

Signals and slots primarily allows communications between objects. Just like in any other GUI programming, when we change / execute a widget, we often want another widget to be notified. Beneath the concept, we want the objects to ba able to communicate with one another.

When a user clicks the close pushbutton, we’d probably want the mainWindow to close, by calling a close()

Now, for most frameworks, (wxwidget, fltk, win32) the only way to achieve this is to use callbacks, basically a pointer to functions. Based from my experience, callbacks inherits 2 fundamental flaws, firstly they are not type safe, meaning that we can never be certain whether the processing function
will call the callback with correct arguments.

Secondly, a callback is strongly linked to the calling function since the processing function must know which callback to call. In large software development, things can go complex when there’s too many callbacks to call.


An example syntax of signals and slots in Qt is shown below:
QObject::connect(&button_1,SIGNAL(clicked()), &application(quit()));

Like the name says, button_1 is given a signal flag, in the form of a click. When the signal is executed, the application will recevie instruction to respond to the signal, by quitting.


One Response

Subscribe to comments with RSS.

  1. Ankur Gupta said, on January 19, 2007 at 3:25 pm

    Well one signal to slot connecting results in on avg 10 function calls … yes you do get the comfort but a huge cost … I would like to draw ur attention to the … read the code and u see how qt wraps callbacks for us …

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: