Connecting signals and slots

Signals and slots can be connected and disconnected dynamically using the QObject::connect() and QObject::disconnect() functions. A regular, signal-slot connection is defined by the following four attributes:

  • An object that changes its state (sender)
  • A signal in the sender object
  • An object that contains the function to be called (receiver)
  • A slot in the receiver

If you want to make the connection, you need to call the QObject::connect function and pass these four parameters to it. For example, the following code can be used to clear the input box whenever the button is clicked on:

connect(button,   &QPushButton::clicked,
        lineEdit, &QLineEdit::clear);

Signals and slots in this code are specified using a standard ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.