Qt5 Signals And Slots New Syntax

Example

The conventional connect syntax that uses SIGNAL and SLOT macros works entirely at runtime, which has two drawbacks: it has some runtime overhead (resulting also in binary size overhead), and there's no compile-time correctness checking. The new syntax addresses both issues. Before checking the syntax in an example, we'd better know what happens in particular.

Let's say we are building a house and we want to connect the cables. This is exactly what connect function does. Signals and slots are the ones needing this connection. The point is if you do one connection, you need to be careful about the further overlaping connections. Whenever you connect a signal to a slot, you are trying to tell the compiler that whenever the signal was emitted, simply invoke the slot function. This is what exactly happens.

Signals and slots were one of the distinguishing features that made Qt an exciting and innovative tool back in time. But sometimes you can teach new tricks to an old dog, and QObjects gained a new way to connect between signals and slots in Qt5, plus some extra features to connect to other functions which are not slots. Connecting overloaded signals/slots. While being better in many regards, the new connection syntax in Qt5 has one big weakness: Connecting overloaded signals and slots. In order to let the compiler resolve the overloads we need to use staticcasts to member function pointers, or (starting in Qt 5.7) qOverload and friends.

Here's a sample main.cpp:

Hint: the old syntax (SIGNAL/SLOT macros) requires that the Qt metacompiler (MOC) is run for any class that has either slots or signals. From the coding standpoint that means that such classes need to have the Q_OBJECT macro (which indicates the necessity to run MOC on this class).

The new syntax, on the other hand, still requires MOC for signals to work, but not for slots. If a class only has slots and no signals, it need not have the Q_OBJECT macro and hence may not invoke the MOC, which not only reduces the final binary size but also reduces compilation time (no MOC call and no subsequent compiler call for the generated *_moc.cpp file).



Related Tags

Application and user interface components need to communicate with each other. For example, a button needs to know that the user has clicked on it. The button may change colors to indicate its state or perform some logic. As well, application needs to know whether the user is clicking the button. The application may need to relay this clicking event to other applications.

QML has a signal and handler mechanism, where the signal is the event and the signal is responded to through a signal handler. When a signal is emitted, the corresponding signal handler is invoked. Placing logic such as scripts or other operations in the handler allows the component to respond to the event.

Receiving Signals with Signal Handlers

To receive a notification when a particular signal is emitted for a particular object, the object definition should declare a signal handler named on<Signal> where <Signal> is the name of the signal, with the first letter capitalized. The signal handler should contain the JavaScript code to be executed when the signal handler is invoked.

For example, the MouseArea type from the QtQuick module has a clicked signal that is emitted whenever the mouse is clicked within the area. Since the signal name is clicked, the signal handler for receiving this signal should be named onClicked. In the example below, whenever the mouse area is clicked, the onClicked handler is invoked, applying a random color to the Rectangle:

Looking at the MouseArea documentation, you can see the clicked signal is emitted with a parameter named mouse which is a MouseEvent object that contains further details about the mouse click event. This name can be referred to in our onClicked handler to access this parameter. For example, the MouseEvent type has x and y coordinates that allows us to print out the exact location where the mouse was clicked:

Property Change Signal Handlers

A signal is automatically emitted when the value of a QML property changes. This type of signal is a property change signal and signal handlers for these signals are written in the form on<Property>Changed where <Property> is the name of the property, with the first letter capitalized.

Signals

For example, the MouseArea type has a pressed property. To receive a notification whenever this property changes, write a signal handler named onPressedChanged:

Even though the MouseArea documentation does not document a signal handler named onPressedChanged, the signal is implicitly provided by the fact that the pressed property exists.

Using the Connections Type

Qt5

In some cases it may be desirable to access a signal outside of the object that emits it. For these purposes, the QtQuick module provides the Connections type for connecting to signals of arbitrary objects. A Connections object can receive any signal from its specified target.

For example, the onClicked handler in the earlier example could have been received by the root Rectangle instead, by placing the onClicked handler in a Connections object that has its target set to the MouseArea:

Attached Signal Handlers

NewQt5 signals and slots new syntax diagram

An attached signal handler is a signal handler that receives a signal from an attaching type rather than the object within which the handler is declared.

For example, Component.onCompleted is an attached signal handler. This handler is often used to execute some JavaScript code when its creation process has been completed, as in the example below:

The onCompleted handler is not responding to some completed signal from the Rectangle type. Instead, an object of the Componentattaching type with a completed signal has automatically been attached to the Rectangle object by the QML engine, and the engine emits this signal when the object is fully created, thus triggering the Component.onCompleted signal handler.

Attached signal handlers allow objects to be notified of particular signals that are significant to each individual object. If there was no Component.onCompleted attached signal handler, for example, then an object could not receive this notification without registering for some special signal from some special object. The attached signal handler mechanism enables objects to receive particular signals without these extra processes.

See Attached properties and attached signal handlers for more information on attached signal handlers.

Qt5 Signals And Slots New Syntax Analyzer

Adding Signals to Custom QML Types

Signals can be added to custom QML types through the signal keyword.

The syntax for defining a new signal is:

signal <name>[([<type> <parameter name>[, ...]])]

A signal is emitted by invoking the signal as a method.

For example, say the code below is defined in a file named SquareButton.qml. The root Rectangle object has an activated signal. When the child MouseArea is clicked, it emits the parent's activated signal with the coordinates of the mouse click:

Qt5 Signals And Slots New Syntax Diagram

Now any objects of the SquareButton can connect to the activated signal using an onActivated signal handler:

See Signal Attributes for more details on writing signals for custom QML types.

Connecting Signals to Methods and Signals

Signal objects have a connect() method to a connect a signal either to a method or another signal. When a signal is connected to a method, the method is automatically invoked whenever the signal is emitted. This mechanism enables a signal to be received by a method instead of a signal handler.

Below, the messageReceived signal is connected to three methods using the connect() method:

In many cases it is sufficient to receive signals through signal handlers rather than using the connect() function. However, using the connect method allows a signal to be received by multiple methods as shown above, which would not be possible with signal handlers as they must be uniquely named. Also, the connect method is useful when connecting signals to dynamically created objects.

There is a corresponding disconnect() method for removing connected signals:

Signal to Signal Connect

By connecting signals to other signals, the connect() method can form different signal chains.

Whenever the MouseAreaclicked signal is emitted, the send signal will automatically be emitted as well.

© 2020 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.