Professional Documents
Culture Documents
Objects
Objects
http://qt.nokia.com
1/33
Module: Objects in Qt
Common Features of Qts Object Model Object Communication using Signals & Slots Signal/Slot Variations Handling Events in Qt
Objects in Qt
2/33
Objects in Qt
3/33
Module: Objects in Qt
Common Features of Qts Object Model Object Communication using Signals & Slots Signal/Slot Variations Handling Events in Qt
Objects in Qt
4/33
Objects in Qt
5/33
Object Tree
QObjects organize themselves in object trees Based on parent-child relationship QObject(QObject *parent = 0) Parent adds object to list of children Parent owns children Construction/Destruction Tree can be constructured in any order Tree can be destroyed in any order
Parent QObject QObject QObject Children
if object has parent: object rst removed from parent if object has children: deletes each child rst No object is deleted twice
Objects in Qt
6/33
Creating Objects
On Heap - QObject with parent:
QLabel *label = new QLabel("Some Text", parent);
children automatically On Stack - QObject without parent: QFile, usually local to a function QApplication (local to main()) Top level QWidgets: QMainWindow On Stack - "value" types
QString name; QStringList list; QColor color;
See QVariant::Type Documentation
Do not inherit QObject Passed by value everywhere Exception: QString is implicitly shared (COW strategy)
QFile
QLabel QPushButton
QTextEdit
Objects in Qt
8/33
Tristate mechanism For hide/show and enable/disable, ensures that e.g. an explicitly hidden child is not shown when the parent is shown.
Demo objects/ex-showhide
Objects in Qt
9/33
Layout Process Add widgets to layout Layouts may be nested Set layout on container widget
Objects in Qt
10/33
Objects in Qt
11/33
Objects in Qt
12/33
Objects in Qt
12/33
Objects in Qt
12/33
Objects in Qt
12/33
Objects in Qt
12/33
Objects in Qt
12/33
Objects in Qt
13/33
Module: Objects in Qt
Common Features of Qts Object Model Object Communication using Signals & Slots Signal/Slot Variations Handling Events in Qt
Objects in Qt
14/33
Callbacks
General Problem
How do you get from "the user clicks a button" to your business logic?
Possible solutions Callbacks
Based on function pointers Not type-safe Observer Pattern (Listener) Based on interface classes Needs listener registraction Many interface classes
Qt uses Signals and slots for high-level (semantic) callbacks Virtual methods for low-level (syntactic) events.
Objects in Qt
15/33
Objects in Qt
16/33
Objects in Qt
17/33
42
Signal emitted
Objects in Qt
17/33
42
Slot implemented
Objects in Qt
17/33
42
42
Signal/Slot connection
Objects in Qt
17/33
42
42
Objects in Qt
17/33
42
42
Objects in Qt
17/33
42
42
Objects in Qt
17/33
42
42
Slot implemented
Demo objects/ex-connect
Objects in Qt
17/33
Custom Slots
File: myclass.h
class MyClass : public QObject { Q_OBJECT // marker for moc // ... public slots: void setValue(int value); // a custom slot };
File: myclass.cpp
void MyClass::setValue(int value) { // slot implementation }
Objects in Qt
18/33
Custom Signals
File: myclass.h
class MyClass : public QObject { Q_OBJECT // marker for moc // ... signals: void valueChanged(int value); // a custom signal };
File: myclass.cpp
// No implementation for a signal
Sending a signal
emit valueChanged(value);
Objects in Qt
19/33
Objects in Qt
20/33
Example
connect(button, SIGNAL(clicked()), this, SLOT(onClicked()));
Objects in Qt
21/33
Objects in Qt
22/33
Module: Objects in Qt
Common Features of Qts Object Model Object Communication using Signals & Slots Signal/Slot Variations Handling Events in Qt
Objects in Qt
Signal/Slot Variations
23/33
connect( m_slider, SIGNAL( valueChanged( int value ) ) this, SLOT( setValue( int newValue ) ) )
Objects in Qt
Signal/Slot Variations
24/33
valueChanged(int)
textChanged(QString) clicked()
Objects in Qt
Signal/Slot Variations
valueChanged(int)
textChanged(QString) clicked()
Objects in Qt
Signal/Slot Variations
valueChanged(int)
textChanged(QString) clicked()
Objects in Qt
Signal/Slot Variations
valueChanged(int)
textChanged(QString) clicked()
Objects in Qt
Signal/Slot Variations
valueChanged(int)
textChanged(QString) clicked()
Objects in Qt
Signal/Slot Variations
valueChanged(int)
textChanged(QString) clicked()
Objects in Qt
Signal/Slot Variations
valueChanged(int)
textChanged(QString) clicked()
Objects in Qt
Signal/Slot Variations
valueChanged(int)
textChanged(QString) clicked()
Objects in Qt
Signal/Slot Variations
valueChanged(int)
textChanged(QString) clicked()
Objects in Qt
Signal/Slot Variations
valueChanged(int)
textChanged(QString) clicked()
Objects in Qt
Signal/Slot Variations
valueChanged(int)
textChanged(QString) clicked()
Objects in Qt
Signal/Slot Variations
need it?
Objects in Qt
Signal/Slot Variations
26/33
need it?
Objects in Qt
Signal/Slot Variations
26/33
need it?
Objects in Qt
Signal/Slot Variations
26/33
need it?
Objects in Qt
Signal/Slot Variations
26/33
need it?
Objects in Qt
Signal/Slot Variations
26/33
need it?
Objects in Qt
Signal/Slot Variations
26/33
need it?
Objects in Qt
Signal/Slot Variations
26/33
need it?
Objects in Qt
Signal/Slot Variations
26/33
Objects in Qt
Signal/Slot Variations
27/33
Module: Objects in Qt
Common Features of Qts Object Model Object Communication using Signals & Slots Signal/Slot Variations Handling Events in Qt
Objects in Qt
Handling Events in Qt
28/33
Event Processing
Qt is an event-driven UI toolkit
QApplication::exec() runs the event loop
1
Generate Events
by input devices: keyboard, mouse, etc. by Qt itself (e.g. timers)
2 3
Queue Events
by event loop
Dispatch Events
by QApplication to receiver: QObject Key events sent to widget with focus Mouse events sent to widget under cursor
Handle Events
by QObject event handler methods
Objects in Qt
Handling Events in Qt
29/33
Event Handling
QObject::event(QEvent *event) Handles all events for this object Specialized event handlers
QWidget::mousePressEvent() for mouse clicks QWidget::keyPressEvent() for key presses
Objects in Qt
Handling Events in Qt
30/33
Objects in Qt
Handling Events in Qt
31/33
Objects in Qt
Handling Events in Qt
32/33
Nokia, Qt and the Nokia and Qt logos are the registered trademarks of Nokia Corporation in Finland and other countries worldwide.
Objects in Qt
Legal
33/33