My first patch on Kate: Signals&Slots

A while ago I started to use Kate’s code as a base on Atelier. Since Kate uses KTextEditor API, and I need to use it, it was quite handy for me.

With that in mind, I noted that all the Signals&Slots of Kate was on the following deprecated pattern of Qt4.

connect(plugin, SIGNAL(newStatus(QString)), m_widget, SLOT(setStatus(QString)));

With that kind of pattern, you had the work to specify if you want a SIGNAL or SLOT with the macros that you see in the example. And for the rest, you needed to add the name and the kind of parameters that the method or signals have. Everything quite clear, right? However, that kind of pattern doesn’t make clear what type the objects connected are and if they have those methods. And the macros convert the parameters on strings, if you make a typo mistake, for example, you won’t discover that on compiling time, and your connection won’t be made.

When Qt5 arrived a new pattern came with it:

 connect(plugin, &KateBtBrowserPlugin::newStatus, m_widget, &KateBtBrowserWidget::setStatus);

You can see quite a difference between them. The first is that you specify the type of the object emitting or receiving the event. Like on this that ‘plugin’ is a KateBtBrowserPlugin and ‘m_widget’ a private member of KateBrowserWidget. That kind of clarity you didn’t have on the previous pattern.

Also, you needed to know all the parameters of the methods that you were connecting, and that is quite verbose. With this new parameter, you just do the connections, straightforward. And if you do a wrong connection, like a typo mistake, you will have a compiling error.

Another change of the patters was that on Qt4 with the specifying of the parameters, you didn’t need to worry about overloaded methods. With Qt5, you need, because the difference between them isn’t clear since you just write the signature. So you need to make a static cast.

connect(m_targetCombo,  SIGNAL(currentIndexChanged(int)), this, SLOT(slotTargetSelected(int)));
connect(m_targetCombo, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &ConfigView::slotTargetSelected);

You may think that this second example is difficult to handle, with every connection that you need to use a slot or signal that is overloaded, you need to make the static cast of the type of the signal/slot that you need. However, since the code is checked in compilation time, the static_cast helps you to get the right signal/slot.

It there’s a lot of more advantages of using Qt5 pattern since this pattern is laying around since 2012. I just explained part of the changes that inspired me to work on this patch for Kate.

I used this post by Olivier Goffart as the base for some examples, and I leave thanks for Helio Chissini that gave me the link to it. I would recommend that you check it so you can see the lambdas use with connects. They are pure love ❤

If you are interested in all the changes that I did for Kate, check the diff here.

That’s all folks!


5 thoughts on “My first patch on Kate: Signals&Slots

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