Qt Concept - SFDV4001 OOP with C++

Download Report

Transcript Qt Concept - SFDV4001 OOP with C++

L8: Qt4 Concept
Qt Module
QObject
QObject Model
Signal and Slot
Qt Event Loop
Qt Modules
The main five Qt 4 modules are


Core: QObject, QThread, QFile, etc.
GUI: all classes derived from QWidget, and some related
classes

XML for parsing and serializing XML

SQL for communicating with SQL databases

NET for communicating data between hosts on specific
protocols.
Qt Object (I)
The base class of all Qt objects.
Its class name can be found via the corresponding QmetaObject::className().
Has a unique QObject::objectName().
Has a location in an object hierarchy.
Can be connected to other Qt Objects to emit signals to them or to receive
signals emitted by them.
Can have new properties added to it at runtime that are not declared in the C++
class.
Q_OBJECT macro is mandatory for any object that implements signals, slots or
properties.
Qt Objects (II)
Qt Objects should be treated as identities, not as
values. Identities are cloned, not copied or
assigned, and cloning an identity is a more
complex operation than copying or assigning a
value.
Therefore, QObject and all subclasses of QObject
(direct or indirect) have their copy constructor
and assignment operator disabled.
Qt Object Model (I)
Qt Object Model adds these features to C++:
A mechanism for seamless object communication called
signals and slots.
Queryable and designable object properties.
Event filters.
Contextual string translation for internationalization.
Qt Object Model (II)
Qt Object Model adds these features to C++:
Interval driven timers that make it possible to integrate many tasks in an
event-driven GUI.
Hierarchical and queryable object trees that organize object ownership in
a natural way.
Quarded pointers (QPointer) that are automatically set to 0 when the
referenced object is destroyed.
A dynamic cast that works across library boundaries.
Signal and Slot (I)
A mechanism for communication between objects
A signal is emitted when a particular event occurs.
A slot is a function that is called in response to a
particular signal.
Signal and Slot (II)
The signals and slots mechanism is type safe: the
signature of a signal must match the signature of the
receiving slot.
A slot may have a shorter signature than the signal it
receives because it can ignore extra arguments.
Since the signatures are compatible, the compiler can
detect type mismatches.
Signal and Slot (III)
Signals and slots are loosely coupled: a class which emits a signal
neither knows nor cares which slots receive the signal.
More than one signal can be connected to a single slot, and a signal
can be connected to many slots (many-to-many).
It is even possible to connect a signal directly to another signal.
(This will emit the second signal immediately whenever the first
is emitted.)
Example: Diagram
Object A emits a signal. A slot in object B is called in
response to this signal.
Example: Header (I)
Example: Header (II)
Example: Source
Example: Main
Qt Event Loop: Basic Concept



QEvent encapsulates the notion of an event.
QEvent is the superclass of several specific event classes
such as QActionEvent, QFileOpenEvent, QMouseEvent, etc.
QEvent objects can be created by the window system in
response to actions of the user (e.g., QMouseEvent), at
specified time intervals (QTimerEvent), or explicitly by an
application program.
Qt Event Loop: Basic Concept
An event loop is a program structure that permits
events to be prioritized, enqueued, and
dispatched to objects.
The event loop generally continues running until a
terminating event occurs (e.g., the user clicks
on Quit).
Qt Event Loop:
The Crowd Goes Wild




A Qt program creates objects, connects them, and then tells
the application to exec(). At that point, the objects can send
information to each other in a variety of ways.
QWidgets send QEvents to other QObjects in response to
user actions such as mouse clicks and keyboard events.
A widget can also respond to events from the window
manager such as repaints, resizes, or close events.
QObjects can transmit information to one another by means
of signals and slots.
References
Qt.Nokia (2011). Qt Reference Documentation [Online]. Available from:
http://doc.qt.nokia.com/4.7/index.html [Accessed: 22 January 2011]
Ezust, A. & Ezust, P. (2006). An Introduction to Design Patterns in C++ with Qt
4. (1st ed.). New York: Prentice Hall.
Blanchette, J. & Summerfield, M. (2008). C++ GUI Programming with Qt 4.
(2nd ed.). New York: Prentice Hall.