[go: up one dir, main page]

0% found this document useful (0 votes)
110 views14 pages

Understanding Signals and Slots in QT

The document provides an overview of the signals and slots mechanism in Qt, which facilitates communication between objects. It explains the differences between old (string-based) and new (functor-based) syntax for connecting signals and slots, including examples of custom signals and slots. Additionally, it covers the requirements for declaring and emitting signals, as well as connecting them to slots or functions.

Uploaded by

Circlips
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views14 pages

Understanding Signals and Slots in QT

The document provides an overview of the signals and slots mechanism in Qt, which facilitates communication between objects. It explains the differences between old (string-based) and new (functor-based) syntax for connecting signals and slots, including examples of custom signals and slots. Additionally, it covers the requirements for declaring and emitting signals, as well as connecting them to slots or functions.

Uploaded by

Circlips
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Understanding Signals and Slots in Qt

Dr. Eng. Chiheb Ameur ABID

/in/chiheb-ameur-abid
chiheb.abid@gmail.com

Juin 2024
Signals and Slots in Qt Chiheb Ameur ABID 2/ 14

Signals and Slots

Overview
D
Signals and slots are a mechanism for communication between objects in Qt.
All classes that inherit from QObject or oneA BofI its subclasses can contain
ur
signals and slots. Chiheb Ameur
A me ABID
The Q_OBJECT macro must also ebe b added within the class.
ih
Signals and slots are loosely hcoupled.
C
Z A class emitting a signal neither knows nor cares which slots will receive
the signal.
Z Similarly, an object receiving a signal does not know which object emitted
the signal.
Signals and Slots in Qt Chiheb Ameur ABID 3/ 14

Signals and Slots

Comparison of the Two Syntaxes


A signal/slot connection must be established using the static connect()
method of the QObject class.
Two syntaxes
Old syntax based on strings. D
New syntax based on functors. A BI
u r
Chiheb Ameur
A me ABID
String-based Functor-based
Type checking
eb
ihRuntime Compile-time
Implicit type conversions
Ch NO YES
Lambda expressions NO YES
Connecting signals to slots
with more arguments (de- YES NO
fault
parameters)
Connecting C++ functions
YES NO
to QML functions
Signals and Slots in Qt Chiheb Ameur ABID 4/ 14

Signals and Slots

Connection : Old Syntax (String-based) (1/2)


A signal/slot connection must be established using the connect() method.
Old syntax (Qt 4 and earlier versions)

bool QObject::connect(const QObject * sender,const char * signal, const QObject *


D
receiver,const char * method, Qt::ConnectionType type=Qt::AutoConnection) const;
A BI
ur
Z
Chiheb Ameur
sender : Pointer to the object A me ABID
emitting the signal.
b
Z hihe to connect.
signal : Name of the signal
Z C object containing the slot.
receiver : Pointer to the
Z slot : Name of the slot to connect.
Z type : Flag specifying the connection type.
The SLOT and SIGNAL macros are used to obtain the string representations of
the signal and slot names to connect.
Signals and Slots in Qt Chiheb Ameur ABID 5/ 14

Signals and Slots

Connection : Old Syntax (String-based) (2/2)

D
1
2
#include <QApplication>
#include <QPushButton> A BI
int main(int argc, char **argv) { ur
me ABID
3
QApplication app{argc, argv}; Chiheb Ameur
4
A
5 QPushButton button{"Quit"}; eb
6
7 // Connect the clicked() signal of C
hih
the button object
8 // to the quit() slot of the app object (the qApp macro can be used)
9 QObject::connect(&button, SIGNAL(clicked()), &app, SLOT(quit()));
10 button.show();
11 return app.exec();
12 }
Signals and Slots in Qt Chiheb Ameur ABID 6/ 14

Signals and Slots

Connection : New Syntax (Functor-based)


New syntax (Qt 5 and later versions) :
D
1
A BI
connect(sender, &Sender::signal, receiver, &Receiver::slot);
connect(sender, &Sender::signal, functor);
2
ur
Chiheb Ameur
A me ABID
Z b
Enables compile-time type echecking, flexible argument types, automatic
ih
Chto any member method.
casting, and connection
For overloaded functions/methods, an explicit cast is required.

The connection is automatically disconnected when the sender is destroyed.


Objects used in the functor must remain alive when the signal is emitted.
Signals and Slots in Qt Chiheb Ameur ABID 7/ 14

Signals and Slots

Example 1 : Connection with the New Syntax

1 #include <QApplication>
2 #include <QPushButton>
3 void quit() {
4 QApplication::quit();
5 }
D
6
7 int main(int argc, char *argv[]) { A BI
QApplication app{argc, argv}; ur
me ABID
8
9 QPushButton button{"Quit"}; Chiheb Ameur
A
10
11 // Connect the clicked() signal of the ih
eb
button object
12 Ch
13 // to the quit() slot of the object
14 QObject::connect(&button, &QPushButton::clicked, &app, &QApplication::quit);
15
16 // to the quit() function
17 QObject::connect(&button, &QPushButton::clicked, &quit);
18
19 // to a lambda expression
20 QObject::connect(&button, &QPushButton::clicked, [](){QApplication::quit();});
21
22 button.show();
23 return app.exec();
24 }
Signals and Slots in Qt Chiheb Ameur ABID 8/ 14

Signals and Slots

Example 2 : Connection with the New Syntax

1 #include <QApplication>
2 #include <QComboBox>
3 #include <QDebug>
QComboBox *combo;
4
D
5
A BI
void mySlot(int index) {
6
ur
7
8 }
qDebug()<<"Index changed: "<<index<<'\n';
Chiheb Ameur
A me ABID
b
9
ihe
10
11
int main(int argc, char *argv[])
QApplication a{argc, argv};
{
Ch
12 combo {new QComboBox{}};
13 combo->addItem("Hello");
14 combo->addItem("Qt");
15 // The QComboBox::currentIndexChanged() method is overloaded
16 // A cast is necessary
17 QObject::connect(combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
mySlot);
18 combo->show();
19 return a.exec();
20 }
Signals and Slots in Qt Chiheb Ameur ABID 9/ 14

Signals and Slots

Custom Signals
It is possible to create custom signals and slots.
To declare a custom signal, use the signals keyword in the class declaration.
Z No return value (thus void).
D
Z No method implementation.
A BI
Emitting a signal :
Chiheb Ameur ur
e ABID
b Am
void emit(const QMetaObject::Signal &signal, ...);
e
ih
Ch
Z signal : The signal to emit.
Z Arguments : The signal’s arguments.

Signals cannot be declared public, protected, or private.


Signals are implicitly declared protected.
Signals can only be emitted from the class itself or from a derived class.
Signals and Slots in Qt Chiheb Ameur ABID 10/ 14

Signals and Slots

Custom Slots
Custom slots are declared and defined as private, protected, or public
methods.
D
BI
Use the slots keyword in the class declaration.
A
Chiheb Ameur ur be called directly like any method.
Since slots are regular methods, they can
e ABID
b Am
ihe
toh multiple slots.
A signal can be connected C
Multiple signals can be connected to a single slot.

When the same signal is connected to multiple slots, the slots are activated in
an arbitrary order.
Signals and Slots in Qt Chiheb Ameur ABID 11/ 14

Signals and Slots

Custom Slots/Signals
Skeleton of a class with custom slots/signals

1 #include <QObject>
2
D
3 class MyClass : public QObject {
Q_OBJECT A BI
ur
4

Chiheb Ameur me ABID


5
6 public slots: A
void mySlot() { * slot code * } b
ihe
7

Ch
8
9 signals:
10 void mySignal(); // A signal has no implementation
11
12 };

The Q_OBJECT macro must be used to generate meta-information for the class,
including :
Z List of signals emitted by the class.
Z List of slots declared by the class.
Z List of class properties.
Signals and Slots in Qt Chiheb Ameur ABID 12/ 14

Signals and Slots

Example : Custom Slots/Signals (1/2)

1 #include <QApplication>
2 #include <QMessageBox>
3
4 class MyClass : public QObject
5 {
D
6 Q_OBJECT
public: A BI
ur
7
8
9 int value() const { return val;} A me ABID
explicit MyClass(QObject *parent = nullptr): QObject{parent}{}
Chiheb Ameur
10 eb
11 signals:
C hih
12 // The signal must be emittedwhen the val attribute is modified
13 void valueChanged(int);
14 private:
15 int val;
16 public slots:
17 // Calling setValue modifies the value of val
18 void setValue(int v) {
19 if (v != val) {
20 val = v;
21 emit valueChanged(v);
22 }
23 }
24 };
Signals and Slots in Qt Chiheb Ameur ABID 13/ 14

Example : Custom Slots/Signals (2/2)

1 void MyMessageBox(const MyClass& a, const MyClass& b, const QString title) {


2 QString message {QString{"%1\na: %2\nb: %3"}.arg(title).arg(a.value()).arg(b.value())};
3 QMessageBox::information(nullptr, "MyClass Information", message, QMessageBox::Ok);
4 }
5
6 int main(int argc, char *argv[]) {
7 QApplication app{argc, argv};
D
BI
8 // Create two MyClass objects
MyClass *a {new MyClass{}};
9
A
10 MyClass *b {new MyClass{}}; ur
11 Chiheb Ameur me ABID
// Connect the signal of a to the slot of b A
12
13 // A change in a sends a signal to b ihe
b
14 Ch
QObject::connect(a, &MyClass::valueChanged, b, &MyClass::setValue);
15 // b.val takes the value 100
16 b->setValue(100);
17 MyMessageBox(*a, *b, "b->setValue(100)");
18 // a.val takes the value 99
19 // Through signal-slot communication, b.val also takes the value 99
20 a->setValue(99);
21 // Display the values of a and b
22 MyMessageBox(*a, *b, "a->setValue(99)");
23 delete a; delete b;
24 return 0;
25 }
Signals and Slots in Qt Chiheb Ameur ABID 14/ 14

Merci pour votre attention

D
A BI
ur
Chiheb Ameur
A me ABID
b
ihe
Ch

You might also like