SlideShare a Scribd company logo
1 of 30
Qt Advanced
Part II




1
Contents
• State Machine Framework
• Animations with a State Machine
• Touch Events
• G
  Gestures




2
Qt Advanced
State Machine Framework




3
Introduction

    • I Qt 4.6 the new State Machine Framework was introduced
      In   4 6 th        St t M hi F         k     i t d    d
    • The idea is to define:
        • The possible states the application can be in, and
              p                    pp                  ,
        • How the application can move from one state to another
            • Animations can also be applied to transitions if needed
            • State change is event driven (e g a signal or mouse press event triggered
                               event-driven (e.g.
              due to the user’s actions)
    • Notice that the classes in the State Machine FW belong in most part
      to the QtCore module – not QtGui!
    • The State Machine FW integrates with Qt’s meta-object system,
      property system and the Animation Framework, as explained later
    • We will take a look at the basic usage of the State Machine FW and
      integrating it with animations



4
Main Classes




               From QtGui




5
Demonstration – The States Application

                             S2
    QPushButton::clicked()                            QPushButton::clicked()




    S1                                                S3
                             QPushButton::clicked()
                             QP hB tt      li k d()




6
Recap: Qt Property System 1(2)
    • Data members of QObject-derived classes can be exposed as properties
    • Properties are defined in class declarations using the Q PROPERTY macro
                                                             Q_PROPERTY
    • Syntax:
         Q_PROPERTY( type name READ getFunction
         [WRITE setFunction ] [RESET resetFunction ]
         [DESIGNABLE bool ] [SCRIPTABLE bool ] [STORED bool ] )
    • Examples:
         Q_PROPERTY( QString title READ title WRITE setTitle )
         Q_PROPERTY( bool enabled READ isEnabled WRITE setEnabled )


    • Properties can be accessed using the functions QObject::property() and
      QObject::setProperty()
         • No need to even know the exact type of the object you are manipulating!
         • If the property named in QObject::setProperty() does not exist, it will be dynamically added to the
           object at run-time
                     run time
    • Many widget members are Qt properties
         • Used e.g. in the animation framework




7
Recap: Qt Property System 2(2)

    // MyObject.h
    class MyObject : public QObject {
     Q_OBJECT
     Q_PROPERTY(QString text READ text WRITE setText)
    public:         // Constructor and destructor omitted for simplicity
     void setText(const QString& text) const { m_text = text; }
     QString text() const { return m_text; }
    p
    private:
     QString m_text;
    };


    // Usage in SomeCode.cpp:
    MyObject* myObject = new MyObject();
    QObject* object = myObject;


    myObject->setText(”Some text”);              // Access either in the normal way...
    object->setProperty(”tex”, ”Other text”); // ... or by using the property system




8
First State Machine 1(4)

    • Conceptually our state machine could be presented as shown below
        • State 1 is the initial state
        • In the next few slides we will fill in the source code and run the application as a
          simple demo
              p
    • Useful base class signals:
        • QAbstractState::entered() and
        • QAbstractState::exited()




                  s1                        button.clicked                             s2




                       button.clicked                              button.clicked
                                                    s3



9
First State Machine 2(4)
• The initial application could be written as follows


     int main(int argc, char *argv[ ])
     {
         QApplication a(argc, argv);


         QWidget window;
         window.setFixedSize(200, 200);


         Q
         QPushButton* button =
            new QPushButton("Switch to state 2", &window);


         window.show();
         return a.exec();
     }




10
First State Machine 3(4)
     • Define the state machine
         • Typically before entering the application event loop
           (QApplication::exec())
         • The machine can be started later if needed
     QStateMachine* machine = new QStateMachine(&window);
     QState* s1 = new QState();
     QState* s2 = new QState();
     QState* s3 = new QState();

     s1->addTransition(button, SIGNAL(clicked()), s2);
     s2 >addTransition(button,
     s2->addTransition(button, SIGNAL(clicked()), s3);
     s3->addTransition(button, SIGNAL(clicked()), s1);

     machine->addState(s1);
     machine->addState(s2);
     machine->addState(s3);

     machine >setInitialState(s1);
     machine->setInitialState(s1);
     machine->start();

11
First State Machine 4(4)
• So far our state machine does not perform any meaningful work
• Typically such work means changing the properties of various QObjects in the
  application
     • For example, the button’s text:
           example      button s

            s1->assignProperty(button, ”text”, ”Switch to state 2”);
            s2->assignProperty(button, ”text”,
            s2 >assignProperty(button ”text” ”Switch to state 3”);
            s3->assignProperty(button, ”text”, ”Switch to state 1”);



        • Questions:
             • How would you make the button move to a different position each time
               the state changes?
             • How about changing the window title to display the current state?



12
Qt Advanced
Animations with a State Machine




13
Animations with a State Machine 1(2)
• As mentioned before, the Animation FW can easily be made to work with the
  State Machine
  St t M hi FW
• Simply a matter of assigning one or more animations for each wanted state
  transition
     • Works the same way for both QSignalTransitions and QEventTransitions
• In this case a property animation itself does not need a start or an end value
     • These come from the states in the state machine
     • Intermediate values can be set as before using the base class
       QVariantAnimation::setKeyValueAt() function




14
Animations with a State Machine 2(2)
     QStateMachine *machine = new QStateMachine();


     QState *s1 = new QState(); machine->addState(s1);
     QState *s2 = new QState(); machine->addState(s2);
     s1->assignProperty(button, "geometry", QRect(0 0 100
     s1 >assignProperty(button "geometry" QRect(0, 0, 100, 30));
     s2->assignProperty(button, "geometry", QRect(250, 250, 100, 30));


     QSignalTransition *t1 = s1->addTransition(button SIGNAL(clicked()) s2);
                        t1   s1 >addTransition(button, SIGNAL(clicked()),
     t1->addAnimation(new QPropertyAnimation(button, "geometry"));


     QSignalTransition *t2 = s2->addTransition(button, SIGNAL(clicked()), s1);
                        t2   s2 >addTransition(button,
     t2->addAnimation(new QPropertyAnimation(button, "geometry"));


                             ( );
     machine->setInitialState(s1);
     machine->start();




15
Default Animations
• If a specific animation is wanted regardless of the state transition, a default
  animation can b set
      i ti         be t
      • E.g. always animate ”pos” property if its value is different in the start and target states
• Animations explicitly set on transitions will take precedence over any default
  animation for the given property


     // Created earlier: QState* s1, s2; QStateMachine* machine;
     s2->assignProperty(object, "fooBar", 2.0);
     s1->addTransition(s2);
     machine->setInitialState(s1);
     machine->addDefaultAnimation(new QPropertyAnimation(object, "fooBar"));




16
Qt Advanced
Touch Events




17
Introduction 1(2)

     • R
       Represented b th class QTouchEvent
               t d by the l
         • Contain touch points (QTouchEvent::TouchPoint)
         • Each touch point has a state (Pressed, Moved, Stationary,
           Released)
     • Generated by a touch screen or a touch pad
         • See QTouchEvent::DeviceType
     • GUI widgets do not accept touch events by default - need to explicitly
       enable them:
         • QWidget::setAttribute(Qt::WA_AcceptTouchEvents)
         • QGraphicsItem::setAcceptTouchEvents(true)
     • Multiple widgets may reveice touch events simultanously



18
Introduction 2(2)

     • Th
       There are three t
                 th    touch event types:
                           h     tt
         • QEvent::TouchBegin,
         • QEvent::TouchUpdate and
           QEvent::TouchUpdate,
         • QEvent::TouchEnd
     • The TouchUpdate and TouchEnd events are sent to the widget that
       accepted the TouchBegin event
     • Qt guarantees that duplicate TouchBegin events will never be sent
       to the same widget
     • Touch events and mouse events are not the same thing!
         • Delivered independently from each other
                         p       y




19
Handling Touch Events 1(2)
• Depending on your widget type, you will need to reimplement either
     • QWidget::event(),
     • QGraphicsItem::sceneEvent(), or
     • QAbstractScrollArea::viewportEvent()
• Use QTouchEvent::touchPoints() to get all touch points contained in the
  event
• The event is accepted by default
     • If your widget is not interested in the event, return false from the event() function
         • Eff ti l th same as calling i
           Effectively the       lli ignore() on it
                                           ()
     • This way the event can be propagated properly




20
Handling Touch Events 2(2)

     MyNewWidget::MyNewWidget(QWidget* parent) : QWidget(parent) {
         setAttribute(Qt::WA_AcceptTouchEvents);   // Enable touch events
     }


     // Override base class event() function to intercept touch events
     bool MyNewWidget::event(QEvent* event) {
         switch (event->type()) {
              case QEvent::TouchBegin:
              case QEvent::TouchUpdate:
              case QEvent::TouchEnd:
              {   // Process the event, return true if consumed, f l
                              h                               d false otherwise
                                                                        h   i
                  return myTouchHandler(event);
              }
         }
         return QWidget::event(event);    // Other events to base class function
     }



21
Propagation and Grouping

     •   With QWid t the event is propagated to the widget’s parent in case it is
              QWidgets,
         explicitly ignored by the widget
     •   With QGraphicsItems, the event is given to the next item under the touch
         point when ignored

     •   A grouping algorithm is used with touch events to
         •   Prevent duplicate events being delivered to the same widget, and
         •   Enable touch event delivery to multiple widgets simultaneously
     •   When an additional touch point is detected, the following happens (in this
         order)
         1) If an active touch point is found in any of the widget’s descendants or ancestors,
            the new point is grouped with the first one in the same event
              •   The widget under the touch point will not receive an event
         2) Otherwise the widget under the touch point receives a new touch event




22
Grouping Examples

     1) N
        New t
            touch point ( 2) when a widget ( hild1) already h one ( 1)
                h i t (P2) h         id t (Child1) l d has        (P1)
         •   Points P1 and P2 are grouped, Parent does not get an event


                  Parent

                      Child1     P1            P2          Child2



     2) New touch point to a sibling widget (
      )           p                g    g (Child2)
                                                 )
         •   An existing point not found on Child2 or its parent/children, P2 is sent as a
             separate touch event to Child2

                  Parent

                      Child1     P1                        Child2     P2


23
Qt Advanced
Gestures




24
Introduction 1(2)

     • G t
       Gestures are typically f
                    t i ll formed f
                                d from a series of events
                                            i    f     t
         • Independently from the input methods used
         • E.g. a movement of mouse, or a touch screen action
              g
         • It is up to the developer to decide how to react to gestures
     • Base class in Qt is QGesture, extended by a few specialized
       standard gesture classes
         • QPanGesture
         • QSwipeGesture
         • QPinchGesture
         • QTapGesture
         • QTapAndHoldGesture
           Q p
     • More ready-made gestures might be provided in the future



25
Introduction 2(2)
• The framework provides the developer with means to create additional custom
  gestures
     t
     • Subclass QGestureRecognizer (and possibly QGesture as well)
     • Implement the needed pure virtual functions
• Standard gestures only work based on QTouchEvents
     • Gestures based on e.g. mouse events must be provided by the developer
• Gestures can be enabled on QWidgets and QGraphicsObjects
     • Implies that meta-objects and the property system are once again heavily utilized
       behind the scenes
                  scenes…




26
Using Standard Gestures 1(3)

     • Si il l t t
       Similarly to touch event h dli
                        h     t handling, th b
                                          the base class event f
                                                    l        t function
                                                                   ti
       needs to be reimplemented
         • QWidget::event()
         • QGraphicsItem::sceneEvent()
     • To indicate that your widget is interested in gestures, call either
         • QWidget::grabGesture(Qt::GestureType) or
           QWidget::grabGesture(Qt::GestureType),
         • QGraphicsItem::grabGesture(Qt::GestureType)
     • Gestures will then be delivered to your widget in a QGestureEvent
         • Event type is QEvent::Gesture
         • Can contain multiple gestures
              • QGesture* QGestureEvent::gesture(Qt::GestureType)
                QGestu e QGestu e e t::gestu e(Qt::Gestu e ype)
              • QList<QGesture*> QGestureEvent::gestures()




27
Using Standard Gestures 2(3)

     MyWidget::MyWidget(QWidget* parent) : QWidget(parent) {
         grabGesture(Qt::PanGesture);    // Qt::GestureType contains an enum value
         grabGesture(Qt::SwipeGesture); // for each standard gesture.
     }


     // Override base class event() function to intercept gesture events
     bool MyWidget::event(QEvent* event) {
         if (event->type() == QEvent::Gesture)
              return myGestureHandler(static_cast<QGestureEvent*>(event));
         // Other events to base class function
         return QWidget::event(event);
     }




28
Using Standard Gestures 3(3)
     bool MyWidget::myGestureHandler(QGestureEvent *event) {
         if (QGesture *swipe = event->gesture(Qt::SwipeGesture))
              swipeTriggered(static_cast<QSwipeGesture *>(swipe));
         else if (QGesture *pan = event->gesture(Qt::PanGesture))
              panTriggered(static_cast<QPanGesture >(pan));
              panTriggered(static cast<QPanGesture *>(pan));
         return true;
     }


     void MyWidget::swipeTriggered(QSwipeGesture *gesture) {
         if (gesture->state() == Qt::GestureFinished) {
              if (gesture->horizontalDirection() == QSwipeGesture::Left)
                  g                                     p
                        goPrevImage();
              else
                        goNextImage();
              update();
         }
     }



29
Summary
• State Machine Framework assists you in creating applications that internally need
  to it h f
  t switch from one state to another
                     t t t      th
     • Can be used together with the Animation Framework to provide animated state
       transitions
• Qt supports multi-touch on all platforms
     • Depends on hardware support, of course
• A group of standard gestures is also provided
     • Pan, swipe, pinch,…
     • Easy to create custom gestures when needed




30

More Related Content

What's hot

QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong? QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong? ICS
 
Qt multi threads
Qt multi threadsQt multi threads
Qt multi threadsYnon Perek
 
Petri Niemi Qt Advanced Part 1
Petri Niemi Qt Advanced Part 1Petri Niemi Qt Advanced Part 1
Petri Niemi Qt Advanced Part 1NokiaAppForum
 
Copy Your Favourite Nokia App with Qt
Copy Your Favourite Nokia App with QtCopy Your Favourite Nokia App with Qt
Copy Your Favourite Nokia App with Qtaccount inactive
 
Necessitas - Qt on Android - from FSCONS 2011
Necessitas - Qt on Android - from FSCONS 2011Necessitas - Qt on Android - from FSCONS 2011
Necessitas - Qt on Android - from FSCONS 2011Johan Thelin
 
Qt Graphics View Framework (Qt Developers Meetup Isreal)
Qt Graphics View Framework (Qt Developers Meetup Isreal)Qt Graphics View Framework (Qt Developers Meetup Isreal)
Qt Graphics View Framework (Qt Developers Meetup Isreal)vitalipe
 
Meet the Widgets: Another Way to Implement UI
Meet the Widgets: Another Way to Implement UIMeet the Widgets: Another Way to Implement UI
Meet the Widgets: Another Way to Implement UIICS
 
Convert Your Legacy OpenGL Code to Modern OpenGL with Qt
Convert Your Legacy OpenGL Code to Modern OpenGL with QtConvert Your Legacy OpenGL Code to Modern OpenGL with Qt
Convert Your Legacy OpenGL Code to Modern OpenGL with QtICS
 
QVariant, QObject — Qt's not just for GUI development
QVariant, QObject — Qt's not just for GUI developmentQVariant, QObject — Qt's not just for GUI development
QVariant, QObject — Qt's not just for GUI developmentICS
 
Qt and QML performance tips & tricks for Qt 4.7
Qt and QML performance tips & tricks for Qt 4.7Qt and QML performance tips & tricks for Qt 4.7
Qt and QML performance tips & tricks for Qt 4.7Pasi Kellokoski
 
Qt for Beginners Part 3 - QML and Qt Quick
Qt for Beginners Part 3 - QML and Qt QuickQt for Beginners Part 3 - QML and Qt Quick
Qt for Beginners Part 3 - QML and Qt QuickICS
 
Technical Deep Dive into the New Prefab System
Technical Deep Dive into the New Prefab SystemTechnical Deep Dive into the New Prefab System
Technical Deep Dive into the New Prefab SystemUnity Technologies
 
The Next Generation Qt Item Views
The Next Generation Qt Item ViewsThe Next Generation Qt Item Views
The Next Generation Qt Item Viewsaccount inactive
 

What's hot (20)

Qt Animation
Qt AnimationQt Animation
Qt Animation
 
Cross Platform Qt
Cross Platform QtCross Platform Qt
Cross Platform Qt
 
QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong? QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong?
 
Qt multi threads
Qt multi threadsQt multi threads
Qt multi threads
 
Petri Niemi Qt Advanced Part 1
Petri Niemi Qt Advanced Part 1Petri Niemi Qt Advanced Part 1
Petri Niemi Qt Advanced Part 1
 
State Machine Framework
State Machine FrameworkState Machine Framework
State Machine Framework
 
Qt Workshop
Qt WorkshopQt Workshop
Qt Workshop
 
Copy Your Favourite Nokia App with Qt
Copy Your Favourite Nokia App with QtCopy Your Favourite Nokia App with Qt
Copy Your Favourite Nokia App with Qt
 
Necessitas - Qt on Android - from FSCONS 2011
Necessitas - Qt on Android - from FSCONS 2011Necessitas - Qt on Android - from FSCONS 2011
Necessitas - Qt on Android - from FSCONS 2011
 
Qt Graphics View Framework (Qt Developers Meetup Isreal)
Qt Graphics View Framework (Qt Developers Meetup Isreal)Qt Graphics View Framework (Qt Developers Meetup Isreal)
Qt Graphics View Framework (Qt Developers Meetup Isreal)
 
Meet the Widgets: Another Way to Implement UI
Meet the Widgets: Another Way to Implement UIMeet the Widgets: Another Way to Implement UI
Meet the Widgets: Another Way to Implement UI
 
Cross Platform Qt
Cross Platform QtCross Platform Qt
Cross Platform Qt
 
Convert Your Legacy OpenGL Code to Modern OpenGL with Qt
Convert Your Legacy OpenGL Code to Modern OpenGL with QtConvert Your Legacy OpenGL Code to Modern OpenGL with Qt
Convert Your Legacy OpenGL Code to Modern OpenGL with Qt
 
Qt Widget In-Depth
Qt Widget In-DepthQt Widget In-Depth
Qt Widget In-Depth
 
QVariant, QObject — Qt's not just for GUI development
QVariant, QObject — Qt's not just for GUI developmentQVariant, QObject — Qt's not just for GUI development
QVariant, QObject — Qt's not just for GUI development
 
Qt and QML performance tips & tricks for Qt 4.7
Qt and QML performance tips & tricks for Qt 4.7Qt and QML performance tips & tricks for Qt 4.7
Qt and QML performance tips & tricks for Qt 4.7
 
Qt for Beginners Part 3 - QML and Qt Quick
Qt for Beginners Part 3 - QML and Qt QuickQt for Beginners Part 3 - QML and Qt Quick
Qt for Beginners Part 3 - QML and Qt Quick
 
Technical Deep Dive into the New Prefab System
Technical Deep Dive into the New Prefab SystemTechnical Deep Dive into the New Prefab System
Technical Deep Dive into the New Prefab System
 
Open gl
Open glOpen gl
Open gl
 
The Next Generation Qt Item Views
The Next Generation Qt Item ViewsThe Next Generation Qt Item Views
The Next Generation Qt Item Views
 

Similar to Qt Advanced - Animations with a State Machine

Best Practices in Qt Quick/QML - Part 1 of 4
Best Practices in Qt Quick/QML - Part 1 of 4Best Practices in Qt Quick/QML - Part 1 of 4
Best Practices in Qt Quick/QML - Part 1 of 4ICS
 
Integrazione QML / C++
Integrazione QML / C++Integrazione QML / C++
Integrazione QML / C++Paolo Sereno
 
Kct guidev3p0
Kct guidev3p0Kct guidev3p0
Kct guidev3p0aanchal19
 
Porting Motif Applications to Qt - Webinar
Porting Motif Applications to Qt - WebinarPorting Motif Applications to Qt - Webinar
Porting Motif Applications to Qt - WebinarICS
 
Porting Motif Applications to Qt - Webinar
Porting Motif Applications to Qt - WebinarPorting Motif Applications to Qt - Webinar
Porting Motif Applications to Qt - WebinarJanel Heilbrunn
 
Quickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop ApplicationsQuickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop ApplicationsClare Macrae
 
Introduction to the Qt State Machine Framework using Qt 6
Introduction to the Qt State Machine Framework using Qt 6Introduction to the Qt State Machine Framework using Qt 6
Introduction to the Qt State Machine Framework using Qt 6ICS
 
Best Practices in Qt Quick/QML - Part III
Best Practices in Qt Quick/QML - Part IIIBest Practices in Qt Quick/QML - Part III
Best Practices in Qt Quick/QML - Part IIIICS
 
Witekio custom modern qt quick components
Witekio custom modern qt quick componentsWitekio custom modern qt quick components
Witekio custom modern qt quick componentsWitekio
 
Unity3D Scripting: State Machine
Unity3D Scripting: State MachineUnity3D Scripting: State Machine
Unity3D Scripting: State MachineSperasoft
 
eBay Pulsar: Real-time analytics platform
eBay Pulsar: Real-time analytics platformeBay Pulsar: Real-time analytics platform
eBay Pulsar: Real-time analytics platformKyoungMo Yang
 
How to Program SmartThings
How to Program SmartThingsHow to Program SmartThings
How to Program SmartThingsJanet Huang
 
Introduction to the Qt Quick Scene Graph
Introduction to the Qt Quick Scene GraphIntroduction to the Qt Quick Scene Graph
Introduction to the Qt Quick Scene GraphICS
 
Qt & Webkit
Qt & WebkitQt & Webkit
Qt & WebkitQT-day
 
[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade Serverless[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade ServerlessKatyShimizu
 
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade ServerlessKatyShimizu
 

Similar to Qt Advanced - Animations with a State Machine (20)

Qt Application Programming with C++ - Part 2
Qt Application Programming with C++ - Part 2Qt Application Programming with C++ - Part 2
Qt Application Programming with C++ - Part 2
 
Best Practices in Qt Quick/QML - Part 1 of 4
Best Practices in Qt Quick/QML - Part 1 of 4Best Practices in Qt Quick/QML - Part 1 of 4
Best Practices in Qt Quick/QML - Part 1 of 4
 
Qt Widgets In Depth
Qt Widgets In DepthQt Widgets In Depth
Qt Widgets In Depth
 
Integrazione QML / C++
Integrazione QML / C++Integrazione QML / C++
Integrazione QML / C++
 
Kct guidev3p0
Kct guidev3p0Kct guidev3p0
Kct guidev3p0
 
Porting Motif Applications to Qt - Webinar
Porting Motif Applications to Qt - WebinarPorting Motif Applications to Qt - Webinar
Porting Motif Applications to Qt - Webinar
 
Porting Motif Applications to Qt - Webinar
Porting Motif Applications to Qt - WebinarPorting Motif Applications to Qt - Webinar
Porting Motif Applications to Qt - Webinar
 
Quickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop ApplicationsQuickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop Applications
 
Input and Interaction
Input and InteractionInput and Interaction
Input and Interaction
 
Introduction to the Qt State Machine Framework using Qt 6
Introduction to the Qt State Machine Framework using Qt 6Introduction to the Qt State Machine Framework using Qt 6
Introduction to the Qt State Machine Framework using Qt 6
 
Best Practices in Qt Quick/QML - Part III
Best Practices in Qt Quick/QML - Part IIIBest Practices in Qt Quick/QML - Part III
Best Practices in Qt Quick/QML - Part III
 
Witekio custom modern qt quick components
Witekio custom modern qt quick componentsWitekio custom modern qt quick components
Witekio custom modern qt quick components
 
Treinamento Qt básico - aula II
Treinamento Qt básico - aula IITreinamento Qt básico - aula II
Treinamento Qt básico - aula II
 
Unity3D Scripting: State Machine
Unity3D Scripting: State MachineUnity3D Scripting: State Machine
Unity3D Scripting: State Machine
 
eBay Pulsar: Real-time analytics platform
eBay Pulsar: Real-time analytics platformeBay Pulsar: Real-time analytics platform
eBay Pulsar: Real-time analytics platform
 
How to Program SmartThings
How to Program SmartThingsHow to Program SmartThings
How to Program SmartThings
 
Introduction to the Qt Quick Scene Graph
Introduction to the Qt Quick Scene GraphIntroduction to the Qt Quick Scene Graph
Introduction to the Qt Quick Scene Graph
 
Qt & Webkit
Qt & WebkitQt & Webkit
Qt & Webkit
 
[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade Serverless[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade Serverless
 
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
 

More from NokiaAppForum

Toshl.com - Od ideje do končnega izdelka
Toshl.com - Od ideje do končnega izdelka Toshl.com - Od ideje do končnega izdelka
Toshl.com - Od ideje do končnega izdelka NokiaAppForum
 
Alexander Oswald The Future of Maemo and Symbian
Alexander Oswald The Future of Maemo and SymbianAlexander Oswald The Future of Maemo and Symbian
Alexander Oswald The Future of Maemo and SymbianNokiaAppForum
 
Dominik Gusenbauer Qt Mobility
Dominik Gusenbauer  Qt MobilityDominik Gusenbauer  Qt Mobility
Dominik Gusenbauer Qt MobilityNokiaAppForum
 
Petri Niemi Qt Web Kit
Petri Niemi Qt Web KitPetri Niemi Qt Web Kit
Petri Niemi Qt Web KitNokiaAppForum
 
Andreas Jakl, Qt Symbian Maemo Quickstart
Andreas Jakl, Qt Symbian Maemo QuickstartAndreas Jakl, Qt Symbian Maemo Quickstart
Andreas Jakl, Qt Symbian Maemo QuickstartNokiaAppForum
 
Mobile Web Development from Scratch
Mobile Web Development from ScratchMobile Web Development from Scratch
Mobile Web Development from ScratchNokiaAppForum
 
Wolfgang Damm Wikitude
Wolfgang Damm WikitudeWolfgang Damm Wikitude
Wolfgang Damm WikitudeNokiaAppForum
 
Miha Lesjak Mobilizing The Web with Web Runtime
Miha Lesjak Mobilizing The Web with Web RuntimeMiha Lesjak Mobilizing The Web with Web Runtime
Miha Lesjak Mobilizing The Web with Web RuntimeNokiaAppForum
 
Jure Sustersic Monetization through Ovi Services
Jure Sustersic Monetization through Ovi ServicesJure Sustersic Monetization through Ovi Services
Jure Sustersic Monetization through Ovi ServicesNokiaAppForum
 
Andreas Jakl Software Development on Nokia Deviceswith Qt
Andreas Jakl Software Development on Nokia Deviceswith QtAndreas Jakl Software Development on Nokia Deviceswith Qt
Andreas Jakl Software Development on Nokia Deviceswith QtNokiaAppForum
 

More from NokiaAppForum (12)

Toshl.com - Od ideje do končnega izdelka
Toshl.com - Od ideje do končnega izdelka Toshl.com - Od ideje do končnega izdelka
Toshl.com - Od ideje do končnega izdelka
 
Razum
RazumRazum
Razum
 
Hello Mobile
Hello MobileHello Mobile
Hello Mobile
 
Alexander Oswald The Future of Maemo and Symbian
Alexander Oswald The Future of Maemo and SymbianAlexander Oswald The Future of Maemo and Symbian
Alexander Oswald The Future of Maemo and Symbian
 
Dominik Gusenbauer Qt Mobility
Dominik Gusenbauer  Qt MobilityDominik Gusenbauer  Qt Mobility
Dominik Gusenbauer Qt Mobility
 
Petri Niemi Qt Web Kit
Petri Niemi Qt Web KitPetri Niemi Qt Web Kit
Petri Niemi Qt Web Kit
 
Andreas Jakl, Qt Symbian Maemo Quickstart
Andreas Jakl, Qt Symbian Maemo QuickstartAndreas Jakl, Qt Symbian Maemo Quickstart
Andreas Jakl, Qt Symbian Maemo Quickstart
 
Mobile Web Development from Scratch
Mobile Web Development from ScratchMobile Web Development from Scratch
Mobile Web Development from Scratch
 
Wolfgang Damm Wikitude
Wolfgang Damm WikitudeWolfgang Damm Wikitude
Wolfgang Damm Wikitude
 
Miha Lesjak Mobilizing The Web with Web Runtime
Miha Lesjak Mobilizing The Web with Web RuntimeMiha Lesjak Mobilizing The Web with Web Runtime
Miha Lesjak Mobilizing The Web with Web Runtime
 
Jure Sustersic Monetization through Ovi Services
Jure Sustersic Monetization through Ovi ServicesJure Sustersic Monetization through Ovi Services
Jure Sustersic Monetization through Ovi Services
 
Andreas Jakl Software Development on Nokia Deviceswith Qt
Andreas Jakl Software Development on Nokia Deviceswith QtAndreas Jakl Software Development on Nokia Deviceswith Qt
Andreas Jakl Software Development on Nokia Deviceswith Qt
 

Recently uploaded

WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 

Recently uploaded (20)

WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Qt Advanced - Animations with a State Machine

  • 2. Contents • State Machine Framework • Animations with a State Machine • Touch Events • G Gestures 2
  • 4. Introduction • I Qt 4.6 the new State Machine Framework was introduced In 4 6 th St t M hi F k i t d d • The idea is to define: • The possible states the application can be in, and p pp , • How the application can move from one state to another • Animations can also be applied to transitions if needed • State change is event driven (e g a signal or mouse press event triggered event-driven (e.g. due to the user’s actions) • Notice that the classes in the State Machine FW belong in most part to the QtCore module – not QtGui! • The State Machine FW integrates with Qt’s meta-object system, property system and the Animation Framework, as explained later • We will take a look at the basic usage of the State Machine FW and integrating it with animations 4
  • 5. Main Classes From QtGui 5
  • 6. Demonstration – The States Application S2 QPushButton::clicked() QPushButton::clicked() S1 S3 QPushButton::clicked() QP hB tt li k d() 6
  • 7. Recap: Qt Property System 1(2) • Data members of QObject-derived classes can be exposed as properties • Properties are defined in class declarations using the Q PROPERTY macro Q_PROPERTY • Syntax: Q_PROPERTY( type name READ getFunction [WRITE setFunction ] [RESET resetFunction ] [DESIGNABLE bool ] [SCRIPTABLE bool ] [STORED bool ] ) • Examples: Q_PROPERTY( QString title READ title WRITE setTitle ) Q_PROPERTY( bool enabled READ isEnabled WRITE setEnabled ) • Properties can be accessed using the functions QObject::property() and QObject::setProperty() • No need to even know the exact type of the object you are manipulating! • If the property named in QObject::setProperty() does not exist, it will be dynamically added to the object at run-time run time • Many widget members are Qt properties • Used e.g. in the animation framework 7
  • 8. Recap: Qt Property System 2(2) // MyObject.h class MyObject : public QObject { Q_OBJECT Q_PROPERTY(QString text READ text WRITE setText) public: // Constructor and destructor omitted for simplicity void setText(const QString& text) const { m_text = text; } QString text() const { return m_text; } p private: QString m_text; }; // Usage in SomeCode.cpp: MyObject* myObject = new MyObject(); QObject* object = myObject; myObject->setText(”Some text”); // Access either in the normal way... object->setProperty(”tex”, ”Other text”); // ... or by using the property system 8
  • 9. First State Machine 1(4) • Conceptually our state machine could be presented as shown below • State 1 is the initial state • In the next few slides we will fill in the source code and run the application as a simple demo p • Useful base class signals: • QAbstractState::entered() and • QAbstractState::exited() s1 button.clicked s2 button.clicked button.clicked s3 9
  • 10. First State Machine 2(4) • The initial application could be written as follows int main(int argc, char *argv[ ]) { QApplication a(argc, argv); QWidget window; window.setFixedSize(200, 200); Q QPushButton* button = new QPushButton("Switch to state 2", &window); window.show(); return a.exec(); } 10
  • 11. First State Machine 3(4) • Define the state machine • Typically before entering the application event loop (QApplication::exec()) • The machine can be started later if needed QStateMachine* machine = new QStateMachine(&window); QState* s1 = new QState(); QState* s2 = new QState(); QState* s3 = new QState(); s1->addTransition(button, SIGNAL(clicked()), s2); s2 >addTransition(button, s2->addTransition(button, SIGNAL(clicked()), s3); s3->addTransition(button, SIGNAL(clicked()), s1); machine->addState(s1); machine->addState(s2); machine->addState(s3); machine >setInitialState(s1); machine->setInitialState(s1); machine->start(); 11
  • 12. First State Machine 4(4) • So far our state machine does not perform any meaningful work • Typically such work means changing the properties of various QObjects in the application • For example, the button’s text: example button s s1->assignProperty(button, ”text”, ”Switch to state 2”); s2->assignProperty(button, ”text”, s2 >assignProperty(button ”text” ”Switch to state 3”); s3->assignProperty(button, ”text”, ”Switch to state 1”); • Questions: • How would you make the button move to a different position each time the state changes? • How about changing the window title to display the current state? 12
  • 13. Qt Advanced Animations with a State Machine 13
  • 14. Animations with a State Machine 1(2) • As mentioned before, the Animation FW can easily be made to work with the State Machine St t M hi FW • Simply a matter of assigning one or more animations for each wanted state transition • Works the same way for both QSignalTransitions and QEventTransitions • In this case a property animation itself does not need a start or an end value • These come from the states in the state machine • Intermediate values can be set as before using the base class QVariantAnimation::setKeyValueAt() function 14
  • 15. Animations with a State Machine 2(2) QStateMachine *machine = new QStateMachine(); QState *s1 = new QState(); machine->addState(s1); QState *s2 = new QState(); machine->addState(s2); s1->assignProperty(button, "geometry", QRect(0 0 100 s1 >assignProperty(button "geometry" QRect(0, 0, 100, 30)); s2->assignProperty(button, "geometry", QRect(250, 250, 100, 30)); QSignalTransition *t1 = s1->addTransition(button SIGNAL(clicked()) s2); t1 s1 >addTransition(button, SIGNAL(clicked()), t1->addAnimation(new QPropertyAnimation(button, "geometry")); QSignalTransition *t2 = s2->addTransition(button, SIGNAL(clicked()), s1); t2 s2 >addTransition(button, t2->addAnimation(new QPropertyAnimation(button, "geometry")); ( ); machine->setInitialState(s1); machine->start(); 15
  • 16. Default Animations • If a specific animation is wanted regardless of the state transition, a default animation can b set i ti be t • E.g. always animate ”pos” property if its value is different in the start and target states • Animations explicitly set on transitions will take precedence over any default animation for the given property // Created earlier: QState* s1, s2; QStateMachine* machine; s2->assignProperty(object, "fooBar", 2.0); s1->addTransition(s2); machine->setInitialState(s1); machine->addDefaultAnimation(new QPropertyAnimation(object, "fooBar")); 16
  • 18. Introduction 1(2) • R Represented b th class QTouchEvent t d by the l • Contain touch points (QTouchEvent::TouchPoint) • Each touch point has a state (Pressed, Moved, Stationary, Released) • Generated by a touch screen or a touch pad • See QTouchEvent::DeviceType • GUI widgets do not accept touch events by default - need to explicitly enable them: • QWidget::setAttribute(Qt::WA_AcceptTouchEvents) • QGraphicsItem::setAcceptTouchEvents(true) • Multiple widgets may reveice touch events simultanously 18
  • 19. Introduction 2(2) • Th There are three t th touch event types: h tt • QEvent::TouchBegin, • QEvent::TouchUpdate and QEvent::TouchUpdate, • QEvent::TouchEnd • The TouchUpdate and TouchEnd events are sent to the widget that accepted the TouchBegin event • Qt guarantees that duplicate TouchBegin events will never be sent to the same widget • Touch events and mouse events are not the same thing! • Delivered independently from each other p y 19
  • 20. Handling Touch Events 1(2) • Depending on your widget type, you will need to reimplement either • QWidget::event(), • QGraphicsItem::sceneEvent(), or • QAbstractScrollArea::viewportEvent() • Use QTouchEvent::touchPoints() to get all touch points contained in the event • The event is accepted by default • If your widget is not interested in the event, return false from the event() function • Eff ti l th same as calling i Effectively the lli ignore() on it () • This way the event can be propagated properly 20
  • 21. Handling Touch Events 2(2) MyNewWidget::MyNewWidget(QWidget* parent) : QWidget(parent) { setAttribute(Qt::WA_AcceptTouchEvents); // Enable touch events } // Override base class event() function to intercept touch events bool MyNewWidget::event(QEvent* event) { switch (event->type()) { case QEvent::TouchBegin: case QEvent::TouchUpdate: case QEvent::TouchEnd: { // Process the event, return true if consumed, f l h d false otherwise h i return myTouchHandler(event); } } return QWidget::event(event); // Other events to base class function } 21
  • 22. Propagation and Grouping • With QWid t the event is propagated to the widget’s parent in case it is QWidgets, explicitly ignored by the widget • With QGraphicsItems, the event is given to the next item under the touch point when ignored • A grouping algorithm is used with touch events to • Prevent duplicate events being delivered to the same widget, and • Enable touch event delivery to multiple widgets simultaneously • When an additional touch point is detected, the following happens (in this order) 1) If an active touch point is found in any of the widget’s descendants or ancestors, the new point is grouped with the first one in the same event • The widget under the touch point will not receive an event 2) Otherwise the widget under the touch point receives a new touch event 22
  • 23. Grouping Examples 1) N New t touch point ( 2) when a widget ( hild1) already h one ( 1) h i t (P2) h id t (Child1) l d has (P1) • Points P1 and P2 are grouped, Parent does not get an event Parent Child1 P1 P2 Child2 2) New touch point to a sibling widget ( ) p g g (Child2) ) • An existing point not found on Child2 or its parent/children, P2 is sent as a separate touch event to Child2 Parent Child1 P1 Child2 P2 23
  • 25. Introduction 1(2) • G t Gestures are typically f t i ll formed f d from a series of events i f t • Independently from the input methods used • E.g. a movement of mouse, or a touch screen action g • It is up to the developer to decide how to react to gestures • Base class in Qt is QGesture, extended by a few specialized standard gesture classes • QPanGesture • QSwipeGesture • QPinchGesture • QTapGesture • QTapAndHoldGesture Q p • More ready-made gestures might be provided in the future 25
  • 26. Introduction 2(2) • The framework provides the developer with means to create additional custom gestures t • Subclass QGestureRecognizer (and possibly QGesture as well) • Implement the needed pure virtual functions • Standard gestures only work based on QTouchEvents • Gestures based on e.g. mouse events must be provided by the developer • Gestures can be enabled on QWidgets and QGraphicsObjects • Implies that meta-objects and the property system are once again heavily utilized behind the scenes scenes… 26
  • 27. Using Standard Gestures 1(3) • Si il l t t Similarly to touch event h dli h t handling, th b the base class event f l t function ti needs to be reimplemented • QWidget::event() • QGraphicsItem::sceneEvent() • To indicate that your widget is interested in gestures, call either • QWidget::grabGesture(Qt::GestureType) or QWidget::grabGesture(Qt::GestureType), • QGraphicsItem::grabGesture(Qt::GestureType) • Gestures will then be delivered to your widget in a QGestureEvent • Event type is QEvent::Gesture • Can contain multiple gestures • QGesture* QGestureEvent::gesture(Qt::GestureType) QGestu e QGestu e e t::gestu e(Qt::Gestu e ype) • QList<QGesture*> QGestureEvent::gestures() 27
  • 28. Using Standard Gestures 2(3) MyWidget::MyWidget(QWidget* parent) : QWidget(parent) { grabGesture(Qt::PanGesture); // Qt::GestureType contains an enum value grabGesture(Qt::SwipeGesture); // for each standard gesture. } // Override base class event() function to intercept gesture events bool MyWidget::event(QEvent* event) { if (event->type() == QEvent::Gesture) return myGestureHandler(static_cast<QGestureEvent*>(event)); // Other events to base class function return QWidget::event(event); } 28
  • 29. Using Standard Gestures 3(3) bool MyWidget::myGestureHandler(QGestureEvent *event) { if (QGesture *swipe = event->gesture(Qt::SwipeGesture)) swipeTriggered(static_cast<QSwipeGesture *>(swipe)); else if (QGesture *pan = event->gesture(Qt::PanGesture)) panTriggered(static_cast<QPanGesture >(pan)); panTriggered(static cast<QPanGesture *>(pan)); return true; } void MyWidget::swipeTriggered(QSwipeGesture *gesture) { if (gesture->state() == Qt::GestureFinished) { if (gesture->horizontalDirection() == QSwipeGesture::Left) g p goPrevImage(); else goNextImage(); update(); } } 29
  • 30. Summary • State Machine Framework assists you in creating applications that internally need to it h f t switch from one state to another t t t th • Can be used together with the Animation Framework to provide animated state transitions • Qt supports multi-touch on all platforms • Depends on hardware support, of course • A group of standard gestures is also provided • Pan, swipe, pinch,… • Easy to create custom gestures when needed 30