Академический Документы
Профессиональный Документы
Культура Документы
• C++ introduction
• Basics of C++ object-oriented programming
Contents
• Qt core features
• Shared data objects
• Object model
• Signals & slots
• Object properties
3
Creating a C++ project
4
Quick start
5
Quick start
6
Quick start
7
Quick start
8
Quick start
9
Quick start
10
Quick start
11
Quick start
• Build with Ctrl+B, run with Ctrl+R
12
Project files overview
QT C++ PROJECTS
13
Qt project file
• A .pro file with same name as the directory
it sits in
14
Qt project basics
• Project name and type
• TARGET, TEMPLATE
• Project files
• Project configuration
• CONFIG, QT
15
Project templates
• Basic TEMPLATE types: app, lib, subdirs
16
Project name
• Project TARGET specifies the output file
name
• TARGET = helloworld
17
Project files
• SOURCES are obviously needed
18
Sources and headers
• QtCreator updates the directives in .pro file
in most cases
19
UI resources
• UI resource files are XML documents, which
are processed by uic compiler during build
20
UI resources
21
Other resources
• Resource file specifies a collection of data
that should be bundled into the binary file
22
Resource files
23
Resource files
24
Resource files
• After resource file has been created, add a
prefix into it and a file under the prefix
• :/<prefix>/<resource-name>
25
Building a Qt project
QT C++ PROJECTS
26
Build from command line
• Run qmake in the directory, which contains
the .pro file
27
Shadow builds
• Interactive part
28
Excercise
• Open the helloworld example into GUI
designer
29
Shared libraries
QT C++ PROJECTS
30
Shared libraries
• A shared library contains code that is
loaded once and shared by all executables
that use it
31
Exporting from project
• In order to be used, a library needs to
provide an API
32
Public headers
• Project file variables
33
Exporting from project
• INSTALLS directive is used to specify what
and where to install
34
Using exported libraries
• To use the library, a project needs to find
the exported data
• -l<library-name>
35
Object-oriented programming with Qt/C++
C++ INTRODUCTION
36
C++ OOP basics
• A class defines the structure of an object
37
C++ OOP basics
• Object data must be initialized in
constructor and freed in destructor
38
C++ OOP basics
• A class may inherit other classes
39
Memory management
• Calling new reserves an area of memory
for the object
40
Memory management
• Objects allocated with new are reserved
from program heap
41
Memory management
• Stack vs. heap Stack
Stack
value
pointer
obj2
&obj
obj
Heap
42
Pointers and references
• An object allocated from heap is referenced
by a pointer or a reference
43
Pointers and references
• Following prints out 10 (why?)
44
Pointers and references
• References are not usually used as
variables within functions
45
Function return values
• Functions return value should always be
thought as a copy
value is 25
46
Notes about const
• The const keyword indicates that
something is not modifiable
47
C++ hands-on
• Create a new Console Application project
48
Shared data objects
CORE FEATURES
49
Shared data objects
• A shared data object doesn’t store the
object data by itself
• Examples:
50
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
51
Implicit sharing
• In implicit sharing, a reference counter is
associated with the data
QString str
Data *
52
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
53
Terminology
• Copy-on-write
• Shallow copy
• Deep copy
54
Strings
• Two types of string
55
Strings and implicit sharing
• Strings are implicitly shared, so in general,
should be treated as a value
56
String operations
• In Qt, a string can be changed
57
String operations
• QString supports various operators
58
Console output
• Qt has qPrintable function, which should be
used when printing strings with qDebug
59
Generic containers
• List containers
• Associative containers
60
C++ templates
• Containers are based on C++ templates
• Some examples:
• QList<QPicture>
• List of pictures
• QHash<QString,QObject>
• Name-to-object dictionary
61
List containers
• Lists are index-based, starting from 0
• Accessing items
• at, ’[]’
62
Foreach statement
• Can be used to iterate over lists
63
Associative containers
• Associative containers are used to map
keys to values
64
Object model and signals & slots
CORE FEATURES
65
Object model
• Usual Qt program is based around a tree-
based hierarchy of objects
66
Object model
• A QObject may have a
parent object and number
of child objects
QObject
children
67
Object model and GUI
• All GUI components inherit from QWidget,
which in turn inherits from QObject
68
Signals and slots
• Qt way of making callback functions simple
• Example cases
• What happens when user presses a GUI button
69
Signals and slots
• Code to support signal-slot connections is
generated by the moc tool when project is
compiled
70
Special keywords
• Q_OBJECT keyword must be added to
every class that inherits from QObject base
class
71
Special keywords
• signals keyword is used to start a block of
signal definitions
72
Special keywords
• slots keyword starts a block of slot
definitions
73
Special keywords
• emit keyword is used to send a notification
to all slots that have been connected to the
signal
74
Connecting signals to slots
• Connections are made with
QObject::connect static functions
• Component-based approach
75
Connecting signals to slots
76
Signals and slots
• Comparing Qt and Java
77
Trying it out
• Open the hellosignalslot example
78
What was done?
• The program event loop was created
79
What was done?
• Two objects were created
80
What was done?
• Objects were connected together
81
What was done?
• Timer and event loop were started
• Quit slot stops the event loop and thus exec function
returns and program quits
82
Short exercise
• Open the hellosignalslot example that
presented in previous slides
83
Object properties
CORE FEATURES
84
Object properties
• All QObject-based classes support
properties
85
Object properties
• Static properties are declared into class
header using Q_PROPERTY macro
86
Object properties
• Properties are used in QML/C++ hybrid
programming
• Monday’s topics
• QML plug-in’s
87
88