<< to CrossControl homepage

Support & Service Center

Use new signal and slot syntax for cleaner code

Printer-friendly versionPrinter-friendly versionPDF versionPDF version

Qt 5 introduced a new syntax for connecting signals with slot which is highly recommended to use. The old syntax is still valid but there are numerous reasons why the new syntax should be used:

  • Compile time check of the existence of the signals and slot, of the types, or if the Q_OBJECT is missing.
  • Argument can be by typedefs or with different namespace specifier, and it works.
  • Possibility to automatically cast the types if there is implicit conversion (e.g. from QString to QVariant)
  • It is possible to connect to any member function of QObject, not only slots.

Old syntax

connect(
    sender, SIGNAL( valueChanged( QString, QString ) ),
    receiver, SLOT( updateValue( QString ) )
);

New syntax

connect(
    sender, &Sender::valueChanged,
    receiver, &Receiver::updateValue
);

For further reading please follow and read this article: New Signal Slot Syntax

Category: 
whatever