Upload
rian
View
231
Download
0
Embed Size (px)
Citation preview
8/15/2019 Signals Slots
http://slidepdf.com/reader/full/signals-slots 1/19
Signals & Slots
8/15/2019 Signals Slots
http://slidepdf.com/reader/full/signals-slots 2/19
Signals & Slots
• Signals and slots is an object to objectcommunication technique in Qt
• Only QObjects can communicate viasignals and slots
• It can be used in both GUI and consoleapplications
8/15/2019 Signals Slots
http://slidepdf.com/reader/full/signals-slots 3/19
To utilise signals & slots
A class should
• inherit from the QObject class orinherit from a class that inherits fromthe QObject class
• include the QO!"#$% macro
8/15/2019 Signals Slots
http://slidepdf.com/reader/full/signals-slots 4/19
Signals and Slots: Definition and Implementation
Signals and slots
• are defined as void member functionsof a class
•
can have input parameter lists
8/15/2019 Signals Slots
http://slidepdf.com/reader/full/signals-slots 5/19
Signals and Slots: Definition and Implementation
A slot
• must be implemented in the class
• can be invoed as a member function
on a object of the class
8/15/2019 Signals Slots
http://slidepdf.com/reader/full/signals-slots 6/19
Signals and Slots: Definition and Implementation
A signal
• does not have an implementation
• cannot be invoed as a member
function
8/15/2019 Signals Slots
http://slidepdf.com/reader/full/signals-slots 7/19
Signals and Slots: Example 1 (non-GUI)
Product definition Product implementation
#ifndef PRODUCT_H#define PRODUCT_H#include <QString>
class Product{ pulic!
Product"QString d int n$% &oid sell"int n$% pri&ate! QString description%
int nu'%(%#endif
Product!!Product"QString dint n$!description"d$ nu'"n${(
&oid Product!!sell"int n${ if"n <) nu'$ nu' ) nu' * n%(
8/15/2019 Signals Slots
http://slidepdf.com/reader/full/signals-slots 8/19
Signals and Slots: Example 1 (non-GUI)
Product definition Product implementation#ifndef PRODUCT_H#define PRODUCT_H#include <QString>#include <QObject>
class Product: public QObject{ Q_OBJECT pulic!
Product"QString d int n$% &oid sell"int n$% public signals:
void restoc!Q"tring d$ pri&ate! QString description% int nu'%(%#endif
Product!!Product"QString dint n$!description"d$ nu'"n${(
&oid Product!!sell"int n${ if"n <) nu'$ nu' ) nu' * n% i% !nu& '' ( e&it restoc!description$(
8/15/2019 Signals Slots
http://slidepdf.com/reader/full/signals-slots 9/19
Signals and Slots: Example 1 (non-GUI)
Order)ist definition Order)ist implementation
#ifndef ORD+R,-ST_H#define ORD+R,-ST_H#include <QString,ist>
class Order,ist{ pulic!
QString,ist get,ist"$const%
&oid add"QString d$%pri&ate! QString,ist descriptions%(%#endif
QString,ist Order,ist!!get,ist"$const{ return descriptions%(
&oid Order,ist!!add"QString d${ descriptions.append"d$%(
8/15/2019 Signals Slots
http://slidepdf.com/reader/full/signals-slots 10/19
Signals and Slots: Example 1 (non-GUI)
Order)ist definition Order)ist implementation#ifndef ORD+R,-ST_H#define ORD+R,-ST_H#include <QString,ist>#include <QObject>
class Order,ist ! publicQObject*Q_OBJECT pulic!
QString,ist get,ist"$const%
public slots: &oid add"QString d$%pri&ate! QString,ist descriptions%(%#endif
QString,ist Order,ist!!get,ist"$const{ return descriptions%
(
&oid Order,ist!!add"QString d${ descriptions.append"d$%(
8/15/2019 Signals Slots
http://slidepdf.com/reader/full/signals-slots 11/19
Signals and Slots: Example 1 (non-GUI)
One last step in this e'ample is toconnect the signal and slots(
&ain! function
Product c/eese""0reen 1ar'" 2$% 33 an instance of Product
Order,ist list% 33 an instance of Order,ist
QO4ect!!connect"5c/eese S-067,"restoc8"QString$$ 5listS,OT"add"QString$$$%
c/eese.sell"2$% 33 results in "0reen 1ar'" eing added to t/e
33 list of descriptions of Order,ist o4ect33 list
8/15/2019 Signals Slots
http://slidepdf.com/reader/full/signals-slots 12/19
Refletion on Example 1
• Signals and slots in a non)GUIprogram
• User)defined classes *ith signals andslots
8/15/2019 Signals Slots
http://slidepdf.com/reader/full/signals-slots 13/19
!ext
+e *ill
• see ho* to use signals and slots in a
GUI program
•use signals and slots from the e'istingQt classes
8/15/2019 Signals Slots
http://slidepdf.com/reader/full/signals-slots 14/19
Re#ui$ements fo$ t%e GUI p$og$am
%he program should generate thefollo*ing GUI
and clicing on the button Clear shouldclear the line edit
8/15/2019 Signals Slots
http://slidepdf.com/reader/full/signals-slots 15/19
Details of t%e GUI
Q,aelQ,ine+dit
QPus/9utton
Q:9o;,aout
,efer to the A-I documentation forfurther details of these classes.
Q=idget
8/15/2019 Signals Slots
http://slidepdf.com/reader/full/signals-slots 16/19
Example : (GUI p$og$am)
++include necessar, pre-processor directives .ereint &ain!int argc/ c.ar 0argv12*
Q3pplication a!argc/ argv$Q4idget 5$Q)abel 0label ' ne5 Q)abel!6Enter Te7t:6$Q)ineEdit 0input ' ne5 Q)ineEdit!68ello6$QPus.Button 0clear ' ne5 QPus.Button!6Clear6$Q9Bo7)a,out 0la,out ' ne5 Q9Bo7)a,out!$la,out->add4idget!label$la,out->add4idget!input$la,out->add4idget!clear$
5set)a,out!la,out$
QObject::connect!clear/ ";=3)!cliced!/ input/")OT!clear!$
5s.o5!$return ae7ec!$
'etsa$e
$eated
*a+out isused to
plae t%e ,idgets
Signal and slotonneted
8/15/2019 Signals Slots
http://slidepdf.com/reader/full/signals-slots 17/19
Refletion on Example
• Signals and slots in a GUI program
• Used signals and slots that alreadye'ist in Qt classes
8/15/2019 Signals Slots
http://slidepdf.com/reader/full/signals-slots 18/19
Summa$+
%his presentation
• e'plained object to object
communication in Qt using signalsand slots
• contains simple e'amples that shouldhelp you understand the basics ofsignals and slots
8/15/2019 Signals Slots
http://slidepdf.com/reader/full/signals-slots 19/19
a+ .o$,a$d//
/ou should e'plore signal and slots byusing them in your console and GUIapplications.
%hat0s the best *ay to understand them1