×
Namespaces

Variants
Actions

Signals and Slots in Qt

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Compatibility
Platform(s):
Symbian
Article
Created: rahulvala (26 Jan 2009)
Last edited: hamishwillee (11 Oct 2012)

This article provides an overview of Qt Signals and Slots. It is intended to be complimentary to the official Qt Reference

Contents

Overview

Signals and slots provide inter-object communication facilities. They are easy to understand and use, and are fully supported by Qt Creator. The signals and slots implementation smoothly extends C++’s syntax and takes full advantage of C++’s object-oriented features. Signals and slots are type-safe, can be overloaded or reimplemented, and may appear in the public, protected, or private sections of a class.

The signals and slots mechanism is a powerful inter-object communication mechanism that can be used to completely replace the crude callbacks and message maps used by legacy toolkits. Signals and slots are flexible, fully object-oriented, and implemented in C++.

To associate some code with a button using the old callback mechanism, it is necessary to pass a function pointer to the button. When the button is clicked, the function is then called. Old toolkits do not ensure that arguments of the correct type are given to the function when it is called, making crashes more likely. Another problem with the callback approach is that it tightly binds the GUI element to the functionality, making it difficult to develop classes independently.

Qt’s signals and slots mechanism is different. Qt widgets emit signals when events occur. For example, a button will emit a “clicked” signal when it is clicked. The programmer can choose to connect to a signal by creating a function (a “slot”) and calling the connect() function to relate the signal to the slot. Qt’s signals and slots mechanism does not require classes to have knowledge of each other, which makes it much easier to develop highly reusable classes. Since signals and slots are type-safe, type errors are reported as warnings and do not cause crashes to occur.

For example, if a Quit button’s clicked() signal is connected to the application’s quit() slot, a user’s click on Quit makes the application terminate. In code, this is written as

 connect(button, SIGNAL(clicked()), qApp, SLOT(quit()));

Connections can be added or removed at any time during the execution of a Qt application, they can be set up so that they are executed when a signal is emitted or queued for later execution, and they can be made between objects in different threads.

Signals and slots model

 QPushButton*  quit=new QPushButton(“Quit”, genitore);
Scritta->setGeometry(10,10,80,30);
 
Qobject::connect(quit, SIGNAL(clicked()), &base, SLOT(QUIT()));
 
Base.setMainWidget(genitore);
Genitore->show();

Slots are almost identical to ordinary C++ member functions. They can be virtual; they can be overloaded; they can be public; protected, or private, they can be directly invoked like any other C++member functions; and their parameters can be of any types. The difference is that a slot can also be connected to a signal, in which case it is automatically called each time the signal is emitted.

The connect() statement looks like this:

 connect(sender, SIGNAL(signal), receiver, SLOT(slot));

where sender and receiver are pointers to QObjects and where signal and slot are function signatures without parameter names. The SIGNAL() and SLOT() macros essentially convert their argument to a string.

Signal1.png


The different possibilities to connect SIGNAL and SLOT are given below :

One signal can be connected to many slots

 connect(slider, SIGNAL(valueChanged(int)),
spinBox, SLOT(setValue(int)));
connect(slider, SIGNAL(valueChanged(int)),
this, SLOT(updateStatusBarIndicator(int)));

When the signal is emitted, the slots are called one after the other, in an unspecified order.

Many signals can be connected to the same slot

 connect(lcd, SIGNAL(overflow()),
this, SLOT(handleMathError()));
connect(calculator, SIGNAL(divisionByZero()),
this, SLOT(handleMathError()));

When either signal is emitted, the slot is called.

A signal can be connected to another signal

 connect(lineEdit, SIGNAL(textChanged(const QString &)),
this, SIGNAL(updateRecord(const QString &)));

When the first signal is emitted, the second signal is emitted as well. Apart from that, signal–signal connections are indistinguishable from signal–slot connections.


Connections can be removed

 disconnect(lcd, SIGNAL(overflow()),
this, SLOT(handleMathError()));

This is rarely needed, because Qt automatically removes all connections involving an object when that object is deleted.

Further Information

For more information about Signals and Slots see: http://doc.qt.nokia.com/latest/signalsandslots.html

This page was last modified on 11 October 2012, at 01:18.
81 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×