Professional Documents
Culture Documents
• Core features
• Object model
• Signals & slots
• Event loop
• Shared data
• Strings
• Containers
3
Quick C++ OOP refresher
• A class defines the structure of an object
4
Quick C++ OOP refresher
• Object data must be initialized in
constructor and freed in destructor
5
Quick C++ OOP refresher
• A class may inherit other classes
6
Memory management
• Calling new reserves an area of memory
for the object
7
Memory management
• Objects allocated with new are reserved
from program heap
8
Memory management
• Stack vs. heap
9
Core features
Object model, signals & slots and event loop
10
Object model
• Usual Qt program is based around a tree-
based hierarchy of objects
11
Object model
• A QObject may have a
parent object and number
of child objects
QObject
children
12
Object model and GUI
• All GUI components inherit from QWidget,
which in turn inherits from QObject
13
Signals and slots
• Qt way of making callback functions simple
• Example cases
• What happens when user presses a GUI button
14
Signals and slots
• Code to support signal-slot connections is
generated by the moc tool when project is
compiled
15
Special keywords
• Q_OBJECT keyword must be added to
every class that inherits from QObject base
class
16
Special keywords
• signals keyword is used to start a block of
signal definitions
17
Special keywords
• slots keyword starts a block of slot
definitions
18
Special keywords
• emit keyword is used to send a notification
to all slots that have been connected to the
signal
19
Connecting signals to slots
• Connections are made with
QObject::connect static functions
• Component-based approach
20
Connecting signals to slots
21
Signals and slots
• Comparing Qt and Java
22
Event loop
• Purpose of event loop is to keep program
running and responsive to whatever
happens
• User interaction
• Basic idea
23
Event processing
• Any Qt object may be a target for events
24
Trying it out
• Create a new console application with
QtCreator
25
Trying it out
26
Trying it out
27
Trying it out
28
What was done?
• The program event loop was created
29
What was done?
• Two objects were created
30
What was done?
• Objects were connected together
31
What was done?
• Timer and event loop were started
• Quit slot stops the event loop and thus exec function
returns and program quits
32
More core features
Shared data objects
33
Shared data objects
• For some objects there’s no use for
features of the object model
• Performance reasons
• Copy-on-write semantics
34
Implicit sharing
• In normal C++ an object is allocated and a
pointer to it is passed around
char *ptr
char *ptr
H e l l o ! \0
char *ptr
35
Implicit sharing
• In implicit sharing, a reference counter is
associated with the data
QString str
Data *
36
Implicit sharing
• Implicitly shared objects can be treated as
simple values
QString str
Data
Data *
int ref = 1 H e l l o ! \0
QString str
Data
Data * int ref = 1 C h a n g e \0
37
Terminology
• Copy-on-write
• Shallow copy
• Deep copy
38
Copy on write
39
Strings
• Two types of string
40
Strings and implicit sharing
• Strings are implicitly shared, so in general,
should be treated as a value
41
String operations
• In Qt, a string can be changed
42
String operations
• QString supports various operators
43
Console output
• Qt has qPrintable function, which should be
used when printing strings with qDebug
44
Generic containers
• List containers
• Associative containers
45
C++ templates
• Containers are based on C++ templates
• Some examples:
• QList<QPicture>
• List of pictures
• QHash<QString,QObject>
• Name-to-object dictionary
46
List containers
• Lists are index-based, starting from 0
• Accessing items
• at, ’[]’
47
Foreach statement
• Can be used to iterate over lists
48
Associative containers
• Associative containers are used to map
keys to values
49
Creating new objects
• Two classes for reference counting
50
Shared data reminders
• Keep things simple
51
Opaque pointers
Planning for the future
52
Opaque pointers
• Private Implementation
• Why?
• Code maintenance
• Hide dirty details from user of the API
53
Memory allocation
• The size of the allocated memory block is
determined at compile time
Updated API
54
Changing implementation
• Sometimes it’s necessary to implement
features in platform-specific ways
55
Opaque pointers
56
Object ownership
• Public object owns the private object
• Allocated in constructor
• Deleted in destructor
57
Implementation options
• Totally separated
58
Implementation options
• Private class within public
API source
59
Opaque pointers
• Implicitly shared objects all implement the
opaque pointer semantics
60
Programming exercise
Music library object model
61
Programming exercise
• Music library
• Relations:
• MusicLibrary has a list of artists
62
Programming exercise
• Object properties (set / get functions)
• Artist:
• Home page (QUrl)
• Record:
• Release date (QDateTime)
• Song:
• Number (int)
63
Programming exercise
• Common properties
• Optional
• Add base class for common functionality
64
Programming exercise
• Optional
65
66