Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks....

44
Qt Essentials - Objects Module Training Course Visit us at http://qt.digia.com Produced by Digia Plc. Material based on Qt 5.0, created on September 27, 2012 Digia Plc.

Transcript of Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks....

Page 1: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Qt Essentials - ObjectsModuleTraining Course

Visit us at http://qt.digia.com

Produced byDigia Plc.

Material based on Qt 5.0, created on September 27, 2012

Digia Plc.

Page 2: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Module: Object Communication

Signals & Slots

Event Handling

2/30

Object CommunicationObject Communication

Page 3: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Module Learning Objectives

• Learn ...• ... how objects communication• ... details of signals & slots• ... which variations for signal/slot connections exist• ... how to create custom signals & slots• ... what the role of theQt event loop is• ... howQt handles events

3/30

Object CommunicationObject Communication

Page 4: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Object Communication

• Between objectsSignals & Slots

• BetweenQt and the applicationEvents

• BetweenObjects on threadsSignal & Slots + Events

• Between ApplicationsDBus, QSharedMemory

4/30

Object CommunicationObject Communication

Page 5: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Module: Object Communication

Signals & Slots

Event Handling

Signals & Slots 5/30

Object CommunicationObject Communication

Page 6: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

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 registration• Many interface classes

• Qt uses• Signals and slots for high-level (semantic) callbacks• Virtual methods for low-level (syntactic) events.

Signals & Slots 6/30

Object CommunicationObject Communication

Page 7: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Connecting Signals to Slots

Signals & Slots 7/30

Object CommunicationObject Communication

Page 8: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Connecting Signals to Slots

Signals & Slots 7/30

Object CommunicationObject Communication

Page 9: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Connecting Signals to Slots

Signals & Slots 7/30

Object CommunicationObject Communication

Page 10: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Connecting Signals to Slots

Signals & Slots 7/30

Object CommunicationObject Communication

Page 11: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Connecting Signals to Slots

Signals & Slots 7/30

Object CommunicationObject Communication

Page 12: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Connecting Signals to Slots

Signals & Slots 7/30

Object CommunicationObject Communication

Page 13: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Connecting Signals to Slots

Signals & Slots 7/30

Object CommunicationObject Communication

Page 14: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Connecting Signals to Slots

.Demo object-communication/ex-connect

Signals & Slots 7/30

Object CommunicationObject Communication

Page 15: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Connection variants

• Qt 4 style:

connect( slider, SIGNAL(valueChanged(int)),

spinbox, SLOT(setValue(int)));

• Using function pointers:

connect( slider, &QSlider::valueChanged,

spinbox, &QSpinBox::setValue );

• Using non-member function:

static void printValue(int value) {...}

connect( slider, &QSignal::valueChanged, &printValue );

• Using C++11 lambda functions:

connect( slider, &QSlider::valueChanged,

[=] (int value) {...} );

Signals & Slots 8/30

Object CommunicationObject Communication

Page 16: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

connect - function pointers

• Qt 5 components

connect( slider, &QSlider::valueChanged,

spinbox, &QSpinBox::setValue );

• Primary choice when connecting objects

√Compile time errors

√No special syntax for slots

√Q_OBJECT not need for slots

X connecting to overloaded slots is hard

.Demo object-communication/ex-connect-function-pointers

Signals & Slots 9/30

Object CommunicationObject Communication

Page 17: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

connect - Qt4 style

• Qt 4 ported code:

connect( slider, SIGNAL(valueChanged(int)),

spinbox, SLOT(setValue(int)));

Receiving object:

X need to declare the slot in a slots section

X need theQ_OBJECTmacro

X need to have moc run on it

X Only run time errors√

overloaded slot are easy√

Existing Qt4 code do not need to be rewritten

.Demo object-communication/ex-connect

Signals & Slots 10/30

Object CommunicationObject Communication

Page 18: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Custom Slots - Qt4 style

• 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

}

.Demo object-communication/ex-stop-watch

Signals & Slots 11/30

Object CommunicationObject Communication

Page 19: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

connect - non-member

• Using non-member functions:

static void printValue(int value) {

qDebug( "value = %d", value );

}

connect( slider, &QSignal::valueChanged, &printValue );

√No slots syntax, noQ_OBJECT, nomoc

√Compile time errors

√Any function, e.g. the return value of std::bind

.Demo object-communication/ex-connect-non-member

Signals & Slots 12/30

Object CommunicationObject Communication

Page 20: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

connect - lambda functions

• Using C++11 lambda functions:

connect( slider, &QSlider::valueChanged,

[=] (int value) { qDebug("%d", value); } );

√No slots syntax, noQ_OBJECT, nomoc

√Compile time errors

√No need for an extra function

.Demo object-communication/ex-connect-lambda

Signals & Slots 13/30

Object CommunicationObject Communication

Page 21: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

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);

.Demo object-communication/ex-quotebutton

Signals & Slots 14/30

Object CommunicationObject Communication

Page 22: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Q_OBJECT - flag forMOC

• Q_OBJECT

• Enhances QObject withmeta-object information• Required for signals• Required for slots when using theQt4way

• moc creates meta-object information

moc -o moc_myclass.cpp myclass.h

c++ -c myclass.cpp; c++ -c moc_myclass.cpp

c++ -o myapp moc_myclass.o myclass.o

• qmake takes care of mocing files for you

Signals & Slots 15/30

Object CommunicationObject Communication

Page 23: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Variations of Signal/Slot Connections

Signal(s) Connect to Slot(s)

one√

many

many√

one

one√

another signal

• Signal to Signal connection

connect(bt, SIGNAL(clicked()), this, SIGNAL(okSignal()));

• Not allowed to name parameters

connect( m_slider, SIGNAL( valueChanged( int value ) )

this, SLOT( setValue( int newValue ) ) )

Signals & Slots 16/30

Object CommunicationObject Communication

Page 24: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Making the Connection

.Rule for Signal/Slot Connection........Can ignore arguments, but not create values from nothing

Signal Slot

rangeChanged(int,int)

√setRange(int,int)√setValue(int)√update()

valueChanged(int)

√setValue(int)√update()

X setRange(int,int)√setValue(float)†

textChanged(QString) X setValue(int)

† Though not for Qt4 connection types

Signals & Slots 17/30

Object CommunicationObject Communication

Page 25: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Lab: Connect to Click

• Create an application as shown here• Clicking on ``Select Color''

updates label with color's name.

• Hints• QColorDialog::getColor() to fetch a color• QColor::name() to get the color name

• Optional• In QColorDialog, honor the user clicking ``cancel'', and provide it with

the current color to start from.• Set the selected color as the label's background

• Hint: see QPalette• Hint: see QWidget::setAutoFillBackground()

.Lab object-communication/lab-selectcolor

Signals & Slots 18/30

Object CommunicationObject Communication

Page 26: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Lab: Source Compatibility

• Implement custom slider• API compatible with QSlider• Shows current value of slider

• To create custom slider• use QSlider and QLabel

• To test slider• main.cpp provides test code• QLCDNumber is part of test code

• Optional:• Discuss pros and cons of inheriting fromQSlider instead of using an

instance in a layout.

.Lab object-communication/lab-slider

Signals & Slots 19/30

Object CommunicationObject Communication

Page 27: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Module: Object Communication

Signals & Slots

Event Handling

Event Handling 20/30

Object CommunicationObject Communication

Page 28: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Event Processing

.Qt is an event-driven UI toolkit........QApplication::exec() runs the event loop

..1 Generate Events• by input devices: keyboard, mouse, etc.• byQt itself (e.g. timers)

..2 Queue Events• by event loop

..3 Dispatch Events• by QApplication to receiver: QObject

• Key events sent to widget with focus• Mouse events sent to widget under cursor

..4 Handle Events• by QObject event handler methods

Event Handling 21/30

Object CommunicationObject Communication

Page 29: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Event Processing

Press Me

QQuickMouseArea

QQuickView

QPA event loop QCoreApplication

QQuickViewQQuickMouseArea

Event Handling 22/30

Object CommunicationObject Communication

Page 30: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Event Processing

QPA event loop QCoreApplication

sendSpontaneousEvent(QObject *, QEvent *)

Event Handling 23/30

Object CommunicationObject Communication

Page 31: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Event Processing

QCoreApplication

1 event (QEvent *)

2 event (...)3 mousePressEvent (...)

QMouseEvent *

QWindow

QQuickView

QQuickCanvas

Event Handling 24/30

Object CommunicationObject Communication

Page 32: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Event Processing

QWindow

QQuickCanvas

QQuickView

QQuickItem

QQuickMouseArea

mousePressEvent (QMouseEvent *)

.Demo object-communication/ex-qml-event-backtrace

Event Handling 25/30

Object CommunicationObject Communication

Page 33: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Event Handling

• QObject::event(QEvent *event)

• Handles all events for this object

• Specialized event handlers for QWidget and QQuickItem:• mousePressEvent() for mouse clicks• touchEvent() for key presses

• Accepting an Event• event->accept() / event->ignore()

• Accepts or ignores the event• Accepted is the default.

• Event propagation• Happens if event is ignored• Might be propagated to parent widget

.Demo object-communication/ex-allevents

Event Handling 26/30

Object CommunicationObject Communication

Page 34: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Example of Event Handling

• QCloseEvent delivered to top level widgets (windows)

• Accepting event allows window to close

• Ignoring event keeps window open

void MyWidget::closeEvent(QCloseEvent *event) {

if (maybeSave()) {

writeSettings();

event->accept(); // close window

} else {

event->ignore(); // keep window

}

}

.Demo object-communication/ex-closeevent

Event Handling 27/30

Object CommunicationObject Communication

Page 35: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Multi threaded object communication

• Signal/slots between threads

• Posting events using

QCoreApplication::postEvent(QObject* receiver,

QEvent* event)

Event Handling 28/30

Object CommunicationObject Communication

Page 36: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Questions And Answers

• How do you connect a signal to a slot?

• Howwould you implement a slot?

• Howwould you emit a signal?

• Can you return a value from a slot?

• When do you need to run qmake?

• Where do you place the Q_OBJECTmacro andwhen do you need it?

• What is the purpose of the event loop

• How does an event make it from the device to an object in Qt?

Event Handling 29/30

Object CommunicationObject Communication

Page 37: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Questions And Answers

• How do you connect a signal to a slot?

• Howwould you implement a slot?

• Howwould you emit a signal?

• Can you return a value from a slot?

• When do you need to run qmake?

• Where do you place the Q_OBJECTmacro andwhen do you need it?

• What is the purpose of the event loop

• How does an event make it from the device to an object in Qt?

Event Handling 29/30

Object CommunicationObject Communication

Page 38: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Questions And Answers

• How do you connect a signal to a slot?

• Howwould you implement a slot?

• Howwould you emit a signal?

• Can you return a value from a slot?

• When do you need to run qmake?

• Where do you place the Q_OBJECTmacro andwhen do you need it?

• What is the purpose of the event loop

• How does an event make it from the device to an object in Qt?

Event Handling 29/30

Object CommunicationObject Communication

Page 39: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Questions And Answers

• How do you connect a signal to a slot?

• Howwould you implement a slot?

• Howwould you emit a signal?

• Can you return a value from a slot?

• When do you need to run qmake?

• Where do you place the Q_OBJECTmacro andwhen do you need it?

• What is the purpose of the event loop

• How does an event make it from the device to an object in Qt?

Event Handling 29/30

Object CommunicationObject Communication

Page 40: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Questions And Answers

• How do you connect a signal to a slot?

• Howwould you implement a slot?

• Howwould you emit a signal?

• Can you return a value from a slot?

• When do you need to run qmake?

• Where do you place the Q_OBJECTmacro andwhen do you need it?

• What is the purpose of the event loop

• How does an event make it from the device to an object in Qt?

Event Handling 29/30

Object CommunicationObject Communication

Page 41: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Questions And Answers

• How do you connect a signal to a slot?

• Howwould you implement a slot?

• Howwould you emit a signal?

• Can you return a value from a slot?

• When do you need to run qmake?

• Where do you place the Q_OBJECTmacro andwhen do you need it?

• What is the purpose of the event loop

• How does an event make it from the device to an object in Qt?

Event Handling 29/30

Object CommunicationObject Communication

Page 42: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Questions And Answers

• How do you connect a signal to a slot?

• Howwould you implement a slot?

• Howwould you emit a signal?

• Can you return a value from a slot?

• When do you need to run qmake?

• Where do you place the Q_OBJECTmacro andwhen do you need it?

• What is the purpose of the event loop

• How does an event make it from the device to an object in Qt?

Event Handling 29/30

Object CommunicationObject Communication

Page 43: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

Questions And Answers

• How do you connect a signal to a slot?

• Howwould you implement a slot?

• Howwould you emit a signal?

• Can you return a value from a slot?

• When do you need to run qmake?

• Where do you place the Q_OBJECTmacro andwhen do you need it?

• What is the purpose of the event loop

• How does an event make it from the device to an object in Qt?

Event Handling 29/30

Object CommunicationObject Communication

Page 44: Qt Essentials - Objects Module - Training Coursewolberg/qt/lectures2/02...Callbacks. GeneralProblem..Howdoyougetfrom"theuserclicksabutton"toyourbusinesslogic? • Possiblesolutions

©Digia Plc.

Digia, Qt and the Digia andQt logos are the registered trademarks of

Digia Plc. in Finland and other countries worldwide.

Event Handling 30/30

Object CommunicationObject Communication