Qt5 Signal Slots

Since QRect and QImage objects can be serialized for transmission via the signals and slots mechanism, they can be sent between threads in this way, making it convenient to use threads in a wide range of situations where built-in types are used. This is a continued tutorial from the previous one, Qt 5 QTcpSocket. We're going to use Signal and Slot mechanism instead of calling functions manually(?). Note: Qt5 document. The QTcpSocket class provides a TCP socket. TCP (Transmission Control Protocol) is a reliable, stream-oriented, connection-oriented transport protocol.

A timer is used to put a call to the 'run' slot onto the application's event loop. When the worker class is finished, it signals the application's quit slot which stops it's event loop and then returns program flow back to the worker object which then returns from the 'run' slot and everything stops. Is there no simpler way of doing this? Nd the index of the signal and of the slot Keep in an internal map which signal is connected to what slots When emitting a signal, QMetaObject::activate is called. It calls qt metacall (generated by moc) with the slot index which call the actual slot. Signals and slots are used for communication between objects. The signals and slots mechanism is a central feature of Qt and probably the part that differs most from the features provided by other frameworks. In GUI programming, when we change one widget, we often want another widget to be notified.

  • PyQt5 Tutorial
  • PyQt5 Useful Resources
  • Selected Reading

Qt5 Signal Slots Games


Unlike a console mode application, which is executed in a sequential manner, a GUI based application is event driven. Functions or methods are executed in response to user’s actions like clicking on a button, selecting an item from a collection or a mouse click etc., called events.

Widgets used to build the GUI interface act as the source of such events. Each PyQt widget, which is derived from QObject class, is designed to emit ‘signal’ in response to one or more events. The signal on its own does not perform any action. Instead, it is ‘connected’ to a ‘slot’. The slot can be any callable Python function.

Using Qt Designer's Signal/Slot Editor

First design a simple form with a LineEdit control and a PushButton.

It is desired that if button is pressed, contents of text box should be erased. The QLineEdit widget has a clear() method for this purpose. Hence, the button’s clicked signal is to be connected to clear() method of the text box.

To start with, choose Edit signals/slots from Edit menu (or press F4). Then highlight the button with mouse and drag the cursor towards the textbox

As the mouse is released, a dialog showing signals of button and methods of slot will be displayed. Select clicked signal and clear() method

The Signal/Slot Editor window at bottom right will show the result −

Save ui and Build and Python code from ui file as shown in the below code −

Generated Python code will have the connection between signal and slot by the following statement −

Run signalslot.py and enter some text in the LineEdit. The text will be cleared if the button is pressed.

Building Signal-slot Connection

Instead of using Designer, you can directly establish signal-slot connection by following syntax −

Suppose if a function is to be called when a button is clicked. Here, the clicked signal is to be connected to a callable function. It can be achieved in any of the following technique −

Example

In the following example, two QPushButton objects (b1 and b2) are added in QDialog window. We want to call functions b1_clicked() and b2_clicked() on clicking b1 and b2 respectively.

When b1 is clicked, the clicked() signal is connected to b1_clicked() function −

When b2 is clicked, the clicked() signal is connected to b2_clicked() function.

The above code produces the following output −

Qt5 Signal Slots App

Output

Qt5 alpha has been released. One of the features which I have been working on is a new syntax for signals and slot.This blog entry will present it.

Here is how you would connect a signal to a slot:

Qt5 Signal Slots

What really happens behind the scenes is that the SIGNAL and SLOT macros will convert their argument to a string. Then QObject::connect() will compare those strings with the introspection data collected by the moc tool.

What's the problem with this syntax?

While working fine in general, we can identify some issues:

  • No compile time check: All the checks are done at run-time by parsing the strings. That means if you do a typo in the name of the signal or the slot, it will compile but the connection will not be made, and you will only notice a warning in the standard output.
  • Since it operates on the strings, the type names of the slot must match exactly the ones of the signal. And they also need to be the same in the header and in the connect statement. This means it won't work nicely if you want to use typedef or namespaces

In the upcoming Qt5, an alternative syntax exist. The former syntax will still work. But you can now also use this new way of connecting your signals to your slots:

Which one is the more beautiful is a matter of taste. One can quickly get used to the new syntax.

Qt5 Signals Slots Threads

So apart from the aesthetic point of view, let us go over some of the things that it brings us:

Compile-time checking

You will get a compiler error if you misspelled the signal or slot name, or if the arguments of your slot do not match those from the signal.
This might save you some time while you are doing some re-factoring and change the name or arguments of signals or slots.

An effort has been made, using static_assert to get nice compile errors if the arguments do not match or of you miss a Q_OBJECT

Arguments automatic type conversion

Not only you can now use typedef or namespaces properly, but you can also connect signalsto slots that take arguments of different types if an implicit conversion is possible

In the following example, we connect a signal that has a QString as a parameter to a slot that takes a QVariant. It works because QVariant has an implicit constructor that takes a QString

Connecting to any function

App

As you might have seen in the previous example, the slot was just declared as publicand not as slot. Qt will indeed call directly the function pointer of the slot, andwill not need moc introspection anymore. (It still needs it for the signal)

But what we can also do is connecting to any function or functor:

This can become very powerful when you associate that with boost or tr1::bind.

C++11 lambda expressions

Qt5 Signal Slots Game

Everything documented here works with the plain old C++98. But if you use compiler that supportsC++11, I really recommend you to use some of the language's new features.Lambda expressions are supportedby at least MSVC 2010, GCC 4.5, clang 3.1. For the last two, you need to pass -std=c++0x asa flag.

You can then write code like:

This allows you to write asynchronous code very easily.

Qt5 Signals Slots Emit

Update: Also have a look what other C++11 features Qt5 offers.

Qt5 Signal Slot

It is time to try it out. Check out the alpha and start playing. Don't hesistate to report bugs.