SlideShare a Scribd company logo
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

PPTX
Untitled presentation(4)
PDF
Using Multi-Touch and Gestures with Qt
PDF
Special Effects with Qt Graphics View
PDF
Scripting Your Qt Application
PDF
Qt State Machine Framework
PDF
05 - Qt External Interaction and Graphics
PDF
02 - Basics of Qt
PDF
Qt Animation
Untitled presentation(4)
Using Multi-Touch and Gestures with Qt
Special Effects with Qt Graphics View
Scripting Your Qt Application
Qt State Machine Framework
05 - Qt External Interaction and Graphics
02 - Basics of Qt
Qt Animation

What's hot (19)

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

Similar to Petri Niemi Qt Advanced Part 2 (20)

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

More from NokiaAppForum (12)

PPT
Toshl.com - Od ideje do končnega izdelka
PDF
PDF
Hello Mobile
PDF
Alexander Oswald The Future of Maemo and Symbian
PDF
Dominik Gusenbauer Qt Mobility
PDF
Petri Niemi Qt Web Kit
PDF
Andreas Jakl, Qt Symbian Maemo Quickstart
PPTX
Mobile Web Development from Scratch
PDF
Wolfgang Damm Wikitude
PDF
Miha Lesjak Mobilizing The Web with Web Runtime
PDF
Jure Sustersic Monetization through Ovi Services
PDF
Andreas Jakl Software Development on Nokia Deviceswith Qt
Toshl.com - Od ideje do končnega izdelka
Hello Mobile
Alexander Oswald The Future of Maemo and Symbian
Dominik Gusenbauer Qt Mobility
Petri Niemi Qt Web Kit
Andreas Jakl, Qt Symbian Maemo Quickstart
Mobile Web Development from Scratch
Wolfgang Damm Wikitude
Miha Lesjak Mobilizing The Web with Web Runtime
Jure Sustersic Monetization through Ovi Services
Andreas Jakl Software Development on Nokia Deviceswith Qt

Recently uploaded (20)

PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Electronic commerce courselecture one. Pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPT
Teaching material agriculture food technology
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Cloud computing and distributed systems.
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
cuic standard and advanced reporting.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Electronic commerce courselecture one. Pdf
Machine learning based COVID-19 study performance prediction
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Teaching material agriculture food technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Understanding_Digital_Forensics_Presentation.pptx
Cloud computing and distributed systems.
Empathic Computing: Creating Shared Understanding
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
cuic standard and advanced reporting.pdf
Modernizing your data center with Dell and AMD
Building Integrated photovoltaic BIPV_UPV.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Diabetes mellitus diagnosis method based random forest with bat algorithm
How UI/UX Design Impacts User Retention in Mobile Apps.pdf

Petri Niemi Qt Advanced Part 2

  • 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