From Qt 5.0 onwards, Qt offers two different ways to write signal-slot connections in C++: The string-based connection syntax and the functor-based connection syntax. There are pros and cons to both syntaxes. The table below summarizes their differences.
- Qt5 Signals And Slots Syntax
- Qt5 Signals And Slots New Syntax Key
- Qt5 Signals And Slots New Syntax Command
String-based | Functor-based | |
---|---|---|
Type checking is done at.. | Run-time | Compile-time |
Can perform implicit type conversions | Y | |
Can connect signals to lambda expressions | Y | |
Can connect signals to slots which have more arguments than the signal (using default parameters) | Y | |
Can connect C++ functions to QML functions | Y |
The following sections explain these differences in detail and demonstrate how to use the features unique to each connection syntax.
Also, while Qt5 maintains backwards-compatibility with Qt4 -style Signals and Slots, that are based on macros, this project makes use of the newer Qt5 semantics, that are based on function pointers, for the sake of favouring new features over old. I can say that on my Debian 9 / Stretch computer, the example works.
Type Checking and Implicit Type Conversions
- Qt 5.5 connect fails using new signals and slots syntax with Intel compiler. XML Word Printable. Testnewqt5connectsyntax.pro 0.0 kB 15 Sep '15.
- Signals and slots are used for communication between objects. The signals and slots mechanism is a central feature of Qt. In GUI programming, when we change one widget, we often want another widget to be notified. More generally, we want objects of any kind to be able to communicate with one another.
String-based connections type-check by comparing strings at run-time. There are three limitations with this approach:
- Connection errors can only be detected after the program has started running.
- Implicit conversions cannot be done between signals and slots.
- Typedefs and namespaces cannot be resolved.
Limitations 2 and 3 exist because the string comparator does not have access to C++ type information, so it relies on exact string matching.
In contrast, functor-based connections are checked by the compiler. The compiler catches errors at compile-time, enables implicit conversions between compatible types, and recognizes different names of the same type.
For example, only the functor-based syntax can be used to connect a signal that carries an int
to a slot that accepts a double
. A QSlider holds an int
value while a QDoubleSpinBox holds a double
value. The following snippet shows how to keep them in sync:
The following example illustrates the lack of name resolution. QAudioInput::stateChanged() is declared with an argument of type 'QAudio::State'. Thus, string-based connections must also specify 'QAudio::State', even if 'State'
is already visible. This issue does not apply to functor-based connections because argument types are not part of the connection.
Qt5 Signals And Slots Syntax
Making Connections to Lambda Expressions
The functor-based connection syntax can connect signals to C++11 lambda expressions, which are effectively inline slots. This feature is not available with the string-based syntax.
In the following example, the TextSender class emits a textCompleted()
signal which carries a QString parameter. Here is the class declaration:
Here is the connection which emits TextSender::textCompleted()
when the user clicks the button:
In this example, the lambda function made the connection simple even though QPushButton::clicked() and TextSender::textCompleted()
have incompatible parameters. In contrast, a string-based implementation would require extra boilerplate code.
Note: The functor-based connection syntax accepts pointers to all functions, including standalone functions and regular member functions. However, for the sake of readability, signals should only be connected to slots, lambda expressions, and other signals.
Connecting C++ Objects to QML Objects
The string-based syntax can connect C++ objects to QML objects, but the functor-based syntax cannot. This is because QML types are resolved at run-time, so they are not available to the C++ compiler.
In the following example, clicking on the QML object makes the C++ object print a message, and vice-versa. Here is the QML type (in QmlGui.qml
):
Here is the C++ class:
Here is the code that makes the signal-slot connections:
Note: All JavaScript functions in QML take parameters of var
type, which maps to the QVariant type in C++.
When the QPushButton is clicked, the console prints, 'QML received: 'Hello from C++!'. Likewise, when the Rectangle is clicked, the console prints, 'C++ received: 'Hello from QML!'.
See Interacting with QML Objects from C++ for other ways to let C++ objects interact with QML objects.
Using Default Parameters in Slots to Connect to Signals with Fewer Parameters
Usually, a connection can only be made if the slot has the same number of arguments as the signal (or less), and if all the argument types are compatible.
The string-based connection syntax provides a workaround for this rule: If the slot has default parameters, those parameters can be omitted from the signal. Ram slot arrangement. When the signal is emitted with fewer arguments than the slot, Qt runs the slot using default parameter values.
Functor-based connections do not support this feature.
Suppose there is a class called DemoWidget
with a slot printNumber()
that has a default argument:
Using a string-based connection, DemoWidget::printNumber()
can be connected to QApplication::aboutToQuit(), even though the latter has no arguments. The functor-based connection will produce a compile-time error:
To work around this limitation with the functor-based syntax, connect the signal to a lambda function that calls the slot. See the section above, Making Connections to Lambda Expressions.
Selecting Overloaded Signals and Slots
With the string-based syntax, parameter types are explicitly specified. As a result, the desired instance of an overloaded signal or slot is unambiguous.
In contrast, with the functor-based syntax, an overloaded signal or slot must be casted to tell the compiler which instance to use.
For example, QLCDNumber has three versions of the display()
slot:
QLCDNumber::display(int)
QLCDNumber::display(double)
QLCDNumber::display(QString)
To connect the int
version to QSlider::valueChanged(), the two syntaxes are:
See also qOverload().
© 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.
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.
Qt5 Signals And Slots New Syntax Key
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).
The following sections explain these differences in detail and demonstrate how to use the features unique to each connection syntax.
Also, while Qt5 maintains backwards-compatibility with Qt4 -style Signals and Slots, that are based on macros, this project makes use of the newer Qt5 semantics, that are based on function pointers, for the sake of favouring new features over old. I can say that on my Debian 9 / Stretch computer, the example works.
Type Checking and Implicit Type Conversions
- Qt 5.5 connect fails using new signals and slots syntax with Intel compiler. XML Word Printable. Testnewqt5connectsyntax.pro 0.0 kB 15 Sep '15.
- Signals and slots are used for communication between objects. The signals and slots mechanism is a central feature of Qt. In GUI programming, when we change one widget, we often want another widget to be notified. More generally, we want objects of any kind to be able to communicate with one another.
String-based connections type-check by comparing strings at run-time. There are three limitations with this approach:
- Connection errors can only be detected after the program has started running.
- Implicit conversions cannot be done between signals and slots.
- Typedefs and namespaces cannot be resolved.
Limitations 2 and 3 exist because the string comparator does not have access to C++ type information, so it relies on exact string matching.
In contrast, functor-based connections are checked by the compiler. The compiler catches errors at compile-time, enables implicit conversions between compatible types, and recognizes different names of the same type.
For example, only the functor-based syntax can be used to connect a signal that carries an int
to a slot that accepts a double
. A QSlider holds an int
value while a QDoubleSpinBox holds a double
value. The following snippet shows how to keep them in sync:
The following example illustrates the lack of name resolution. QAudioInput::stateChanged() is declared with an argument of type 'QAudio::State'. Thus, string-based connections must also specify 'QAudio::State', even if 'State'
is already visible. This issue does not apply to functor-based connections because argument types are not part of the connection.
Qt5 Signals And Slots Syntax
Making Connections to Lambda Expressions
The functor-based connection syntax can connect signals to C++11 lambda expressions, which are effectively inline slots. This feature is not available with the string-based syntax.
In the following example, the TextSender class emits a textCompleted()
signal which carries a QString parameter. Here is the class declaration:
Here is the connection which emits TextSender::textCompleted()
when the user clicks the button:
In this example, the lambda function made the connection simple even though QPushButton::clicked() and TextSender::textCompleted()
have incompatible parameters. In contrast, a string-based implementation would require extra boilerplate code.
Note: The functor-based connection syntax accepts pointers to all functions, including standalone functions and regular member functions. However, for the sake of readability, signals should only be connected to slots, lambda expressions, and other signals.
Connecting C++ Objects to QML Objects
The string-based syntax can connect C++ objects to QML objects, but the functor-based syntax cannot. This is because QML types are resolved at run-time, so they are not available to the C++ compiler.
In the following example, clicking on the QML object makes the C++ object print a message, and vice-versa. Here is the QML type (in QmlGui.qml
):
Here is the C++ class:
Here is the code that makes the signal-slot connections:
Note: All JavaScript functions in QML take parameters of var
type, which maps to the QVariant type in C++.
When the QPushButton is clicked, the console prints, 'QML received: 'Hello from C++!'. Likewise, when the Rectangle is clicked, the console prints, 'C++ received: 'Hello from QML!'.
See Interacting with QML Objects from C++ for other ways to let C++ objects interact with QML objects.
Using Default Parameters in Slots to Connect to Signals with Fewer Parameters
Usually, a connection can only be made if the slot has the same number of arguments as the signal (or less), and if all the argument types are compatible.
The string-based connection syntax provides a workaround for this rule: If the slot has default parameters, those parameters can be omitted from the signal. Ram slot arrangement. When the signal is emitted with fewer arguments than the slot, Qt runs the slot using default parameter values.
Functor-based connections do not support this feature.
Suppose there is a class called DemoWidget
with a slot printNumber()
that has a default argument:
Using a string-based connection, DemoWidget::printNumber()
can be connected to QApplication::aboutToQuit(), even though the latter has no arguments. The functor-based connection will produce a compile-time error:
To work around this limitation with the functor-based syntax, connect the signal to a lambda function that calls the slot. See the section above, Making Connections to Lambda Expressions.
Selecting Overloaded Signals and Slots
With the string-based syntax, parameter types are explicitly specified. As a result, the desired instance of an overloaded signal or slot is unambiguous.
In contrast, with the functor-based syntax, an overloaded signal or slot must be casted to tell the compiler which instance to use.
For example, QLCDNumber has three versions of the display()
slot:
QLCDNumber::display(int)
QLCDNumber::display(double)
QLCDNumber::display(QString)
To connect the int
version to QSlider::valueChanged(), the two syntaxes are:
See also qOverload().
© 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.
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.
Qt5 Signals And Slots New Syntax Key
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).