You are on page 1of 19

Signals & Slots

Signals & Slots


Signals and slots is an object to object
communication technique in Qt
Only QObjects can communicate via
signals and slots
It can be used in both GUI and console
applications

To utilise signals & slots


A class should
inherit from the QObject class or
inherit from a class that inherits from
the QObject class
include the Q_OBJECT macro

Signals and Slots: Definition and Implementation

Signals and slots


are defined as void member functions
of a class
can have input parameter lists

Signals and Slots: Definition and Implementation

A slot
must be implemented in the class
can be invoked as a member function
on a object of the class

Signals and Slots: Definition and Implementation

A signal
does not have an implementation
cannot be invoked as a member
function

Signals and Slots: Example 1 (non-GUI)


Product definition

Product implementation

#ifndef PRODUCT_H
#define PRODUCT_H
#include <QString>

Product::Product(QString d,
int n):description(d), num(n)
{}

class Product{
public:
Product(QString d, int n);
void sell(int n);
private:
QString description;
int num;
};
#endif

void Product::sell(int n){


if(n <= num)
num = num n;
}

Signals and Slots: Example 1 (non-GUI)


Product definition
#ifndef PRODUCT_H
#define PRODUCT_H
#include <QString>
#include <QObject>
class Product: public QObject{
Q_OBJECT
public:
Product(QString d, int n);
void sell(int n);
public signals:
void restock(QString d);
private:
QString description;
int num;
};
#endif

Product implementation
Product::Product(QString d,
int n):description(d), num(n)
{}
void Product::sell(int n){
if(n <= num)
num = num n;
if (num == 0)
emit restock(description);
}

Signals and Slots: Example 1 (non-GUI)


OrderList definition
#ifndef ORDERLIST_H
#define ORDERLIST_H
#include <QStringList>
class OrderList{
public:
QStringList getList()
const;
void add(QString d);
private:
QStringList descriptions;
};
#endif

OrderList implementation
QStringList OrderList::getList()
const{
return descriptions;
}
void OrderList::add(QString d){
descriptions.append(d);
}

Signals and Slots: Example 1 (non-GUI)


OrderList definition
#ifndef ORDERLIST_H
#define ORDERLIST_H
#include <QStringList>
#include <QObject>
class OrderList : public
QObject{
Q_OBJECT
public:
QStringList getList()
const;
public slots:
void add(QString d);
private:
QStringList descriptions;
};
#endif

OrderList implementation
QStringList OrderList::getList()
const{
return descriptions;
}
void OrderList::add(QString d){
descriptions.append(d);
}

Signals and Slots: Example 1 (non-GUI)


One last step in this example is to
connect the signal and slots:
main() function
Product cheese("Green Farm", 3); // an instance of Product
OrderList list; // an instance of OrderList
QObject::connect(&cheese, SIGNAL(restock(QString)), &list,
SLOT(add(QString)));
cheese.sell(3); // results in "Green Farm" being added to the
// list of descriptions of OrderList object
// list

Reflection on Example 1
Signals and
program

slots

in

non-GUI

User-defined classes with signals and


slots

Next
We will
see how to use signals and slots in a
GUI program
use signals and slots from the existing
Qt classes

Requirements for the GUI program


The program
following GUI

should

generate

the

and clicking on the button Clear should


clear the line edit

Details of the GUI

QLabel
QVBoxLayout

QLineEdit
QPushButton

QWidget

Refer to the API documentation for


further details of these classes.

Example 2: (GUI program)


//include necessary pre-processor directives here
Objects
int main(int argc, char *argv[]){
are
QApplication a(argc, argv);
created
QWidget w;
QLabel *label = new QLabel("Enter Text:");
QLineEdit *input = new QLineEdit("Hello");
Layout is
QPushButton *clear = new QPushButton("Clear");
used to
QVBoxLayout *layout = new QVBoxLayout();
place the
layout->addWidget(label);
widgets
layout->addWidget(input);
layout->addWidget(clear);
w.setLayout(layout);
QObject::connect(clear, SIGNAL(clicked()), input,
SLOT(clear()));
w.show();
return a.exec();
Signal and slot
}
connected

Reflection on Example 2
Signals and slots in a GUI program
Used signals and slots that already
exist in Qt classes

Summary
This presentation
explained object to object
communication in Qt using signals
and slots
contains simple examples that should
help you understand the basics of
signals and slots

Way Forward..

You should explore signal and slots by


using them in your console and GUI
applications.
Thats the best way to understand them!

You might also like