Tuesday, February 8, 2011

Signal and Slot

The concept of Windows Message on Windows SDK was a way too complicated and too hard to program. You have to understand tons of message and some times when you take a look on documentation provided by Microsoft, you are usually not be able to start coding without the sample code. Yup, everybody depend on sample code and hey... how many of you really know what is going on... Perhaps only 1 of 100 programmer know what is really going on, the 99 of them do not really care... It's a shame, isn't it...

But don't blame the programmer, blame the people who write the documentation. With a lot of term which is hard to understand, most of the programmer might be put off and just copy and paste the sample code to get the job done.

But wait a minute, what is the relation between Windows Message and Qt Framework...

Thanks to Qt Framework, you will no longer see Windows Message. Everything will be wrapped and presented to you as Signal and Slot. So forgot about that complicated Windows Message and its documentation.

What is Signal?
If you compare it with Windows SDK, the Signal is the Windows Message. But instead of declare it as a define (eq. #define WM_ACTIVATE 0x0006), the Signal is declared as part of your class declaration. Here is the example:

For Windows Message you might write is as:

#define WM_LINK_CLICKED WM_USER | 0x01

#define WM_LINK_HOVERED WM_USER | 0x02


And for Qt, you will write it as:

class FormOne : public QDialog
{
Q_OBJECT

public:
FormOne(QWidget *parent);
~FormOne();

signals:
void LinkClicked();
void LinkHovered(const QString &);

};


As you can see, as compare to Windows Message, you no need the documentation in order to know what parameter is passed to the message LinkClicked or LinkHovered. From the above declaration we know that LinkClicked message has no parameter and Link Hovered has 1 parameter which is QString.

For Windows Message, you will usually call PostMessage or SendMessage to send the message. On Qt, you have to do the following:
emit LinkClicked();
or
emit LinkHovered("the string to pass to the message handler");

So now you really see the different from the ease of usage.

How about Slot? What is that?
The slot is the message handler. Usually if you code using Windows SDK, you will have a big switch and decide what shall we do for a specific message.

For Qt, the message handler must be declared as slot as the following:

class FormOne : public QDialog
{
Q_OBJECT

public:
FormOne(QWidget *parent);
~FormOne();

signals:
void LinkClicked();
void LinkHovered(const QString &);

private slots:
void OnLinkClicked();
void OnLinkHovered(const QString &);
};


And to connect the signal and the slot you have to call a QObject::connect.
Here is the example of the constructor of the class above.

FormOne(QWidget *parent) : QDialog(parent)
{
QObject::connect (
this,
SIGNAL(LinkClicked()),
this,
SLOT(OnClickClicked())
);


QObject::connect (
this,
SIGNAL(LinkHovered(const QString &)),
this,
SLOT(LinkHovered(const QString &))
);

}


So thats all you need to do...

And you can connect the signal of one object to the slot of the other object as well, so it is not constraint to itself.

To learn more about Signal and Slot, you can visit the following link.

So enough for now...

No comments:

Post a Comment