Signals and slots are a way to publish and receive messages between objects. It is Qt's implementation of the Pub/Sub pattern or the Observer pattern.
A signal is pushed from one object and received in another objects slot.
Qt's model view pattern used for its widget is powered by this functionality.
A signal has a parameter list but no function body. It is emitted by the object of the class.
A typical binding is done in the following way:
bool QObject::connect(senderQObjectPtr, SIGNAL(signalName(parameters)), receiverQObjectPtr, SLOT(slotName(parameters);
There is an optional connection type that I wont go into.
To add signals and slots to your class you need to inherit from the QObject class and include the QObject macro.
Typical header and class definition.
#ifndef SENDER_H
#define SENDER_H
#include <QString>
#include <QObject>
class Sender : public QObject
{
Q_OBJECT
public:
Sender(QObject *parent = 0);
public slots:
void tweetListener(QString tweet);
signals:
void tweet(QString t);
};
#include "receiver.h"
#include <stdio.h>
Receiver::Receiver(QObject *parent) : QObject(parent)
{
}
void Receiver::tweetListener()
{
printf("Hello World \n");
}
Signals are neither public nor private.
Resources:
- Example project in repo
- Ezust Chapter 8 (page 275)
- Voidrealms GUI version https://www.youtube.com/watch?v=JtyCM4BTbYo
- Voidrealms non-GUI version https://www.youtube.com/watch?v=zpZJ4vd7YqY
- Voidrealms revisit https://www.youtube.com/watch?v=qEGRYYx0RBw