SlideShare a Scribd company logo
1 of 66
Download to read offline
Qt State Machine Framework
                             09/25/09
About Me (Kent Hansen)

• Working on Qt since 2005
• QtScript
• Qt State Machine framework
• Plays harmonica and Irish whistle




                                      2
Goals For This Talk

• Introduce the Qt State Machine Framework (SMF)
• Show how to incorporate it in your Qt application
• Inspire you to think about how you would use it




                                                      3
Agenda

• State machines – what and why?
• Statecharts tour
• Qt State Machine tour
• Wrap-up




                                   4
Qt State Machine Framework (SMF)

• Introduced in Qt 4.6
• Part of QtCore module (ubiquitous)
• Originated from Qt-SCXML research project




                                              5
Qt State Machine Framework (SMF)

• Provides C++ API for creating hierarchical finite
 state machines (HFSMs)
• Provides “interpreter” for executing HFSMs




                                                      6
State Chart XML (SCXML)

• “A general-purpose event-based state machine
 language”
• W3C draft (http://www.w3.org/TR/scxml/)
   –Defines tags and attributes
   –Defines algorithm for interpretation




                                                 7
Statecharts – Some use cases

• State-based (“fluid”) UIs
• Asynchronous communication
• AI
• Gesture recognition
• Controller of Model-View-Controller
• Your new, fancy product (e.g. “Hot dog oven”)



                                                  8
Mmm, hot dogs...




                   9
Why State Machines in Qt? (I)




                ?

                                10
Why State Machines in Qt? (II)

• Program = Structure + Behavior
• C++: Structure is language construct
• C++: Event-driven behavior is not language
 construct




                                               11
Why State Machines in Qt? (III)

• Qt already has event-based infrastructure
• Event representation (QEvent)
• Event dispatch
• Event handlers
• So what's the problem?




                                              12
13
The spaghetti code incident (I)

“On button clicked:
if X, do this
else, do that”




                                  14
The spaghetti code incident (II)

“On button clicked:
if X, do this
else, if Y or Z
       if I and not J do that
       else, do that other thing
else, go and have a nap”




                                   15
ifs can get iffy; whiles can get wiley

• if-statements --> state is implicit
• Control flow and useful work jumbled together
• Hard to understand and maintain
• Hard to extend




                                                  16
Can we do better...?




                       17
Qt SMF Mission




 It shouldn't be your job to implement a general-
           purpose HFSM framework!




                                                    18
There's flow...




                  19
… and there's control




                        20
What's in it for YOU?

• Write more robust code
• Have your design and implementation speak the
 same language
• Cope with incremental complexity




                                                  21
Qt + State Machines = Very Good Fit

• Natural extension to Qt's application model
• Integration with meta-object system
• Nested states fit nicely with Qt ownership model




                                                     22
The right tool for the job...




                                23
When NOT to use Qt SMF?

• Lexical analysis, parsing, image decoding
• When performance is critical
• When abstraction level becomes too low
• Not everything should be implemented as a (Qt)
 state machine!




                                                   24
Agenda

• State machines – what and why?
• Statecharts overview
• Qt State Machine tour
• Wrap-up




                                   25
Statecharts overview

• Composite (nested) states
• Behavorial inheritance
• History states




                              26
A composite state




                    27
A composite state decomposed




                               28
“Get ready” state decomposed




                               29
“Speak” state decomposed




                           30
Composite states

• “Zoom in”: Consider more details
• “Zoom out”: Abstract away
• Black box vs white box




                                     31
Like Father, Like Son...




                           32
Behavioral Inheritance

• States implicitly “inherit” the transitions of their
 ancestor state(s)
• Enables grouping and specialization
• Analogue: Class-based inheritance




                                                         33
Behavioral Inheritance example (I)




                                     34
Behavioral Inheritance example (II)




                                      35
History matters...




                     36
History states

• Provide “pause and resume” functionality
• State machine remembers active state
• State machine restores last active state




                                             37
History state example from real life




                                       38
History state example




                        39
Agenda

• State machines – what and why?
• Statecharts overview
• Qt State Machine tour
• Wrap-up




                                   40
Qt State Machine tour

• API introduction w/ small example
• Events and transitions
• Working with states
• Using state machines in your application




                                             41
Qt State Machine API

• Classes for representing states
• Classes for representing transitions
• Classes for state machine-specific events
• QStateMachine class (container & interpreter)




                                                  42
My First State Machine




                         43
My First State Machine




            “Show me the code!”




                                  44
State machine set-up recipe

• Create QStateMachine
• Create states
• Create transitions between states
• Hook up to state signals (entered(), finished())
• Set the initial state
• Start the machine



                                                     45
State machine event processing

• State machine runs its own event loop
• QEvent-based
• Use QStateMachine::postEvent() to post an event




                                                    46
Transitions (I)

• Abstract base class: QAbstractTransition
   –bool eventTest(QEvent*);
• Has zero or more target states
• Add to source state using QState::addTransition()




                                                      47
Transitions (II)

• Convenience for Qt signal transitions:
 addTransition(object, signal, targetState)
• Standard Qt event (e.g. QMouseEvent) transitions
 also supported
  – QEventTransition




                                                     48
Responding to state changes

• QAbstractState::entered() signal
• QAbstractState::exited() signal
• QAbstractTransition::triggered() signal
• QState::finished() signal




                                            49
Composite states

• Follows Qt object hierarchy
• Pass parent state to state constructor


 QState *s1 = new QState();
 QState *s11 = new QState(s1);
 QState *s12 = new QState(s1);
 QFinalState *s13 = new QFinalState(s1);




                                           50
Paralell state group

• Set the state's childMode property



 QState *s1 = new QState();
 s1->setChildMode(QState::ParallelStates);
 QState *s11 = new QState(s1);
 QState *s12 = new QState(s1);




                                             51
History states

• QHistoryState class
• Create as child of composite state
• Use the history state as target of a transition

 QState *s1 = new QState();
 QHistoryState *s1h = new QHistoryState(s1);
 …
 s2->addTransition(foo, SIGNAL(bar()), s1h);


                                                    52
How to use state machines...?




                                53
Scenario: Game (I)

• Many different types of game objects
• Each type's behavior modeled as composite state
• Events trigger transitions
   – Input (e.g. key press)
• States operate on the game object
   –Setting properties (e.g. velocity)
   –Calling slots




                                                    54
Scenario: Game (II)

• Each game object has its own state machine
• The machines run independently
• Separate, top-level state machine that
 “orchestrates”
   –Game menus & modes
   –Start/quit




                                               55
Scenario: Game (III)

• Presence of a state machine is encapsulated
• Up to each type of object
• “Simple” objects don't need to use a state machine




                                                       56
States and animations

• Integrates with Qt animation framework (also new
 in Qt 4.6)
• QAbstractTransition::addAnimation()
• Almost all Qt animation examples use Qt SMF




                                                     57
My tips (I)

• Use the meta-object system integration
   –assignProperty(object, propertyName, value)
   –entered() and exited() signals




                                                  58
My tips (II)

• Use composition
   –Build complex behavior from simple states
   –Take advantage of behavioral inheritance!
   –Don't subclass unnecessarily




                                                59
My tips (III)

• Always draw the statechart first
   –Visualizing the design from C++ is hard
   –The statechart is the design document




                                              60
Agenda

• State machines – what and why?
• Statecharts tour
• Qt State Machine tour
• Wrap-up




                                   61
Summary (I)

• Statecharts are a powerful tool for modeling
 complex, event-driven systems
  –General-purpose
  –Well-defined semantics




                                                 62
Summary (II)

• With the Qt State Machine Framework, you can
 build and run statecharts
• Write more robust code
• You need to consider when/where/how to use it




                                                  63
The Future (Research)

• Qt-SCXML to become part of Qt?
• Qt state machine compiler
• Visual design tool?
• Your feedback matters!




                                   64
Relevant resources
●   http://labs.qt.nokia.com
●   http://lists.trolltech.com
●   Qt Quarterly issue 30
●   irc.freenode.net: #qt-labs




                                 65
Thank You!

Questions?




             66

More Related Content

What's hot

Qt Internationalization
Qt InternationalizationQt Internationalization
Qt InternationalizationICS
 
Basics of Model/View Qt programming
Basics of Model/View Qt programmingBasics of Model/View Qt programming
Basics of Model/View Qt programmingICS
 
Best Practices in Qt Quick/QML - Part 4
Best Practices in Qt Quick/QML - Part 4Best Practices in Qt Quick/QML - Part 4
Best Practices in Qt Quick/QML - Part 4ICS
 
Qt for beginners part 1 overview and key concepts
Qt for beginners part 1   overview and key conceptsQt for beginners part 1   overview and key concepts
Qt for beginners part 1 overview and key conceptsICS
 
Container Security
Container SecurityContainer Security
Container SecuritySalman Baset
 
Qt Framework Events Signals Threads
Qt Framework Events Signals ThreadsQt Framework Events Signals Threads
Qt Framework Events Signals ThreadsNeera Mital
 
Lessons Learned from Building 100+ C++/Qt/QML Devices
Lessons Learned from Building 100+ C++/Qt/QML DevicesLessons Learned from Building 100+ C++/Qt/QML Devices
Lessons Learned from Building 100+ C++/Qt/QML DevicesICS
 
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
 
In-Depth Model/View with QML
In-Depth Model/View with QMLIn-Depth Model/View with QML
In-Depth Model/View with QMLICS
 
Qt test framework
Qt test frameworkQt test framework
Qt test frameworkICS
 
[Webinar] Qt Test-Driven Development Using Google Test and Google Mock
[Webinar] Qt Test-Driven Development Using Google Test and Google Mock[Webinar] Qt Test-Driven Development Using Google Test and Google Mock
[Webinar] Qt Test-Driven Development Using Google Test and Google MockICS
 
Qt multi threads
Qt multi threadsQt multi threads
Qt multi threadsYnon Perek
 
Best Practices in Qt Quick/QML - Part 3
Best Practices in Qt Quick/QML - Part 3Best Practices in Qt Quick/QML - Part 3
Best Practices in Qt Quick/QML - Part 3ICS
 
Best Practices in Qt Quick/QML - Part IV
Best Practices in Qt Quick/QML - Part IVBest Practices in Qt Quick/QML - Part IV
Best Practices in Qt Quick/QML - Part IVICS
 
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
 
Intégration continue
Intégration continueIntégration continue
Intégration continueKlee Group
 
Practical QML - Key Navigation, Dynamic Language and Theme Change
Practical QML - Key Navigation, Dynamic Language and Theme ChangePractical QML - Key Navigation, Dynamic Language and Theme Change
Practical QML - Key Navigation, Dynamic Language and Theme ChangeBurkhard Stubert
 

What's hot (20)

Qt programming-using-cpp
Qt programming-using-cppQt programming-using-cpp
Qt programming-using-cpp
 
Qt Internationalization
Qt InternationalizationQt Internationalization
Qt Internationalization
 
Basics of Model/View Qt programming
Basics of Model/View Qt programmingBasics of Model/View Qt programming
Basics of Model/View Qt programming
 
Best Practices in Qt Quick/QML - Part 4
Best Practices in Qt Quick/QML - Part 4Best Practices in Qt Quick/QML - Part 4
Best Practices in Qt Quick/QML - Part 4
 
Qt for beginners part 1 overview and key concepts
Qt for beginners part 1   overview and key conceptsQt for beginners part 1   overview and key concepts
Qt for beginners part 1 overview and key concepts
 
Container Security
Container SecurityContainer Security
Container Security
 
Introduction to Robot Framework
Introduction to Robot FrameworkIntroduction to Robot Framework
Introduction to Robot Framework
 
Qt Framework Events Signals Threads
Qt Framework Events Signals ThreadsQt Framework Events Signals Threads
Qt Framework Events Signals Threads
 
Lessons Learned from Building 100+ C++/Qt/QML Devices
Lessons Learned from Building 100+ C++/Qt/QML DevicesLessons Learned from Building 100+ C++/Qt/QML Devices
Lessons Learned from Building 100+ C++/Qt/QML Devices
 
Qemu
QemuQemu
Qemu
 
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
 
In-Depth Model/View with QML
In-Depth Model/View with QMLIn-Depth Model/View with QML
In-Depth Model/View with QML
 
Qt test framework
Qt test frameworkQt test framework
Qt test framework
 
[Webinar] Qt Test-Driven Development Using Google Test and Google Mock
[Webinar] Qt Test-Driven Development Using Google Test and Google Mock[Webinar] Qt Test-Driven Development Using Google Test and Google Mock
[Webinar] Qt Test-Driven Development Using Google Test and Google Mock
 
Qt multi threads
Qt multi threadsQt multi threads
Qt multi threads
 
Best Practices in Qt Quick/QML - Part 3
Best Practices in Qt Quick/QML - Part 3Best Practices in Qt Quick/QML - Part 3
Best Practices in Qt Quick/QML - Part 3
 
Best Practices in Qt Quick/QML - Part IV
Best Practices in Qt Quick/QML - Part IVBest Practices in Qt Quick/QML - Part IV
Best Practices in Qt Quick/QML - Part IV
 
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
 
Intégration continue
Intégration continueIntégration continue
Intégration continue
 
Practical QML - Key Navigation, Dynamic Language and Theme Change
Practical QML - Key Navigation, Dynamic Language and Theme ChangePractical QML - Key Navigation, Dynamic Language and Theme Change
Practical QML - Key Navigation, Dynamic Language and Theme Change
 

Similar to Qt State Machine Framework

Petri Niemi Qt Advanced Part 2
Petri Niemi Qt Advanced Part 2Petri Niemi Qt Advanced Part 2
Petri Niemi Qt Advanced Part 2NokiaAppForum
 
Petri Niemi Qt Advanced Part 1
Petri Niemi Qt Advanced Part 1Petri Niemi Qt Advanced Part 1
Petri Niemi Qt Advanced Part 1NokiaAppForum
 
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
 
Optimizing Performance in Qt-Based Applications
Optimizing Performance in Qt-Based ApplicationsOptimizing Performance in Qt-Based Applications
Optimizing Performance in Qt-Based Applicationsaccount inactive
 
Intro to Kubernetes & GitOps Workshop
Intro to Kubernetes & GitOps WorkshopIntro to Kubernetes & GitOps Workshop
Intro to Kubernetes & GitOps WorkshopWeaveworks
 
Scripting Your Qt Application
Scripting Your Qt ApplicationScripting Your Qt Application
Scripting Your Qt Applicationaccount inactive
 
HGZ Kaffeemaschinen & Qt Speak Coffee
HGZ Kaffeemaschinen & Qt Speak CoffeeHGZ Kaffeemaschinen & Qt Speak Coffee
HGZ Kaffeemaschinen & Qt Speak Coffeeaccount inactive
 
Animation Framework: A Step Towards Modern UIs
Animation Framework: A Step Towards Modern UIsAnimation Framework: A Step Towards Modern UIs
Animation Framework: A Step Towards Modern UIsaccount inactive
 
Using Multi-Touch and Gestures with Qt
Using Multi-Touch and Gestures with QtUsing Multi-Touch and Gestures with Qt
Using Multi-Touch and Gestures with Qtaccount inactive
 
下午3 intel fenghaitao_mee_go api and application development
下午3 intel fenghaitao_mee_go api and application development下午3 intel fenghaitao_mee_go api and application development
下午3 intel fenghaitao_mee_go api and application developmentcsdnmobile
 
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
 
Advanced Visualization with OpenGL in Oil & Gas
Advanced Visualization with OpenGL in Oil & GasAdvanced Visualization with OpenGL in Oil & Gas
Advanced Visualization with OpenGL in Oil & Gasaccount inactive
 
High Performance Relaying of C++11 Objects Across Processes and Logic-Labeled...
High Performance Relaying of C++11 Objects Across Processes and Logic-Labeled...High Performance Relaying of C++11 Objects Across Processes and Logic-Labeled...
High Performance Relaying of C++11 Objects Across Processes and Logic-Labeled...vladestivillcastro
 
Plasmaquick Workshop - FISL 13
Plasmaquick Workshop - FISL 13Plasmaquick Workshop - FISL 13
Plasmaquick Workshop - FISL 13Daker Fernandes
 
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
 

Similar to Qt State Machine Framework (20)

Petri Niemi Qt Advanced Part 2
Petri Niemi Qt Advanced Part 2Petri Niemi Qt Advanced Part 2
Petri Niemi Qt Advanced Part 2
 
Petri Niemi Qt Advanced Part 1
Petri Niemi Qt Advanced Part 1Petri Niemi Qt Advanced Part 1
Petri Niemi Qt Advanced Part 1
 
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 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
 
Optimizing Performance in Qt-Based Applications
Optimizing Performance in Qt-Based ApplicationsOptimizing Performance in Qt-Based Applications
Optimizing Performance in Qt-Based Applications
 
Intro to Kubernetes & GitOps Workshop
Intro to Kubernetes & GitOps WorkshopIntro to Kubernetes & GitOps Workshop
Intro to Kubernetes & GitOps Workshop
 
Scripting Your Qt Application
Scripting Your Qt ApplicationScripting Your Qt Application
Scripting Your Qt Application
 
cpp-2013 #18 Qt Part 2
cpp-2013 #18 Qt Part 2cpp-2013 #18 Qt Part 2
cpp-2013 #18 Qt Part 2
 
QtQuick Day 1
QtQuick Day 1QtQuick Day 1
QtQuick Day 1
 
HGZ Kaffeemaschinen & Qt Speak Coffee
HGZ Kaffeemaschinen & Qt Speak CoffeeHGZ Kaffeemaschinen & Qt Speak Coffee
HGZ Kaffeemaschinen & Qt Speak Coffee
 
Animation Framework: A Step Towards Modern UIs
Animation Framework: A Step Towards Modern UIsAnimation Framework: A Step Towards Modern UIs
Animation Framework: A Step Towards Modern UIs
 
Using Multi-Touch and Gestures with Qt
Using Multi-Touch and Gestures with QtUsing Multi-Touch and Gestures with Qt
Using Multi-Touch and Gestures with Qt
 
下午3 intel fenghaitao_mee_go api and application development
下午3 intel fenghaitao_mee_go api and application development下午3 intel fenghaitao_mee_go api and application development
下午3 intel fenghaitao_mee_go api and application development
 
Quantum programming
Quantum programmingQuantum programming
Quantum programming
 
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
 
Advanced Visualization with OpenGL in Oil & Gas
Advanced Visualization with OpenGL in Oil & GasAdvanced Visualization with OpenGL in Oil & Gas
Advanced Visualization with OpenGL in Oil & Gas
 
High Performance Relaying of C++11 Objects Across Processes and Logic-Labeled...
High Performance Relaying of C++11 Objects Across Processes and Logic-Labeled...High Performance Relaying of C++11 Objects Across Processes and Logic-Labeled...
High Performance Relaying of C++11 Objects Across Processes and Logic-Labeled...
 
Plasmaquick Workshop - FISL 13
Plasmaquick Workshop - FISL 13Plasmaquick Workshop - FISL 13
Plasmaquick Workshop - FISL 13
 
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
 

More from account inactive

KDE Plasma for Mobile Phones
KDE Plasma for Mobile PhonesKDE Plasma for Mobile Phones
KDE Plasma for Mobile Phonesaccount inactive
 
Shipping Mobile Applications Using Qt for Symbian
Shipping Mobile Applications Using Qt for SymbianShipping Mobile Applications Using Qt for Symbian
Shipping Mobile Applications Using Qt for Symbianaccount inactive
 
Special Effects with Qt Graphics View
Special Effects with Qt Graphics ViewSpecial Effects with Qt Graphics View
Special Effects with Qt Graphics Viewaccount inactive
 
Developments in The Qt WebKit Integration
Developments in The Qt WebKit IntegrationDevelopments in The Qt WebKit Integration
Developments in The Qt WebKit Integrationaccount inactive
 
Qt on Real Time Operating Systems
Qt on Real Time Operating SystemsQt on Real Time Operating Systems
Qt on Real Time Operating Systemsaccount inactive
 
Development with Qt for Windows CE
Development with Qt for Windows CEDevelopment with Qt for Windows CE
Development with Qt for Windows CEaccount inactive
 
Translating Qt Applications
Translating Qt ApplicationsTranslating Qt Applications
Translating Qt Applicationsaccount inactive
 
Mobile Development with Qt for Symbian
Mobile Development with Qt for SymbianMobile Development with Qt for Symbian
Mobile Development with Qt for Symbianaccount inactive
 
How to Make Your Qt App Look Native
How to Make Your Qt App Look NativeHow to Make Your Qt App Look Native
How to Make Your Qt App Look Nativeaccount inactive
 
Debugging Qt, Fixing and Contributing a Bug Report (Using Gitorious)
Debugging Qt, Fixing and Contributing a Bug Report (Using Gitorious)Debugging Qt, Fixing and Contributing a Bug Report (Using Gitorious)
Debugging Qt, Fixing and Contributing a Bug Report (Using Gitorious)account inactive
 
The Next Generation Qt Item Views
The Next Generation Qt Item ViewsThe Next Generation Qt Item Views
The Next Generation Qt Item Viewsaccount inactive
 
Case Study: Using Qt to Develop Advanced GUIs & Advanced Visualization Software
Case Study: Using Qt to Develop Advanced GUIs & Advanced Visualization SoftwareCase Study: Using Qt to Develop Advanced GUIs & Advanced Visualization Software
Case Study: Using Qt to Develop Advanced GUIs & Advanced Visualization Softwareaccount inactive
 
Case Study: Porting Qt for Embedded Linux on Embedded Processors
Case Study: Porting Qt for Embedded Linux on Embedded ProcessorsCase Study: Porting Qt for Embedded Linux on Embedded Processors
Case Study: Porting Qt for Embedded Linux on Embedded Processorsaccount inactive
 

More from account inactive (20)

Meet Qt
Meet QtMeet Qt
Meet Qt
 
KDE Plasma for Mobile Phones
KDE Plasma for Mobile PhonesKDE Plasma for Mobile Phones
KDE Plasma for Mobile Phones
 
Shipping Mobile Applications Using Qt for Symbian
Shipping Mobile Applications Using Qt for SymbianShipping Mobile Applications Using Qt for Symbian
Shipping Mobile Applications Using Qt for Symbian
 
The Future of Qt Widgets
The Future of Qt WidgetsThe Future of Qt Widgets
The Future of Qt Widgets
 
Special Effects with Qt Graphics View
Special Effects with Qt Graphics ViewSpecial Effects with Qt Graphics View
Special Effects with Qt Graphics View
 
Developments in The Qt WebKit Integration
Developments in The Qt WebKit IntegrationDevelopments in The Qt WebKit Integration
Developments in The Qt WebKit Integration
 
Qt Kwan-Do
Qt Kwan-DoQt Kwan-Do
Qt Kwan-Do
 
Qt on Real Time Operating Systems
Qt on Real Time Operating SystemsQt on Real Time Operating Systems
Qt on Real Time Operating Systems
 
Development with Qt for Windows CE
Development with Qt for Windows CEDevelopment with Qt for Windows CE
Development with Qt for Windows CE
 
Translating Qt Applications
Translating Qt ApplicationsTranslating Qt Applications
Translating Qt Applications
 
Qt Creator Bootcamp
Qt Creator BootcampQt Creator Bootcamp
Qt Creator Bootcamp
 
Qt Widget In-Depth
Qt Widget In-DepthQt Widget In-Depth
Qt Widget In-Depth
 
Mobile Development with Qt for Symbian
Mobile Development with Qt for SymbianMobile Development with Qt for Symbian
Mobile Development with Qt for Symbian
 
How to Make Your Qt App Look Native
How to Make Your Qt App Look NativeHow to Make Your Qt App Look Native
How to Make Your Qt App Look Native
 
Debugging Qt, Fixing and Contributing a Bug Report (Using Gitorious)
Debugging Qt, Fixing and Contributing a Bug Report (Using Gitorious)Debugging Qt, Fixing and Contributing a Bug Report (Using Gitorious)
Debugging Qt, Fixing and Contributing a Bug Report (Using Gitorious)
 
The Mobility Project
The Mobility ProjectThe Mobility Project
The Mobility Project
 
The Next Generation Qt Item Views
The Next Generation Qt Item ViewsThe Next Generation Qt Item Views
The Next Generation Qt Item Views
 
Qt Licensing Explained
Qt Licensing ExplainedQt Licensing Explained
Qt Licensing Explained
 
Case Study: Using Qt to Develop Advanced GUIs & Advanced Visualization Software
Case Study: Using Qt to Develop Advanced GUIs & Advanced Visualization SoftwareCase Study: Using Qt to Develop Advanced GUIs & Advanced Visualization Software
Case Study: Using Qt to Develop Advanced GUIs & Advanced Visualization Software
 
Case Study: Porting Qt for Embedded Linux on Embedded Processors
Case Study: Porting Qt for Embedded Linux on Embedded ProcessorsCase Study: Porting Qt for Embedded Linux on Embedded Processors
Case Study: Porting Qt for Embedded Linux on Embedded Processors
 

Recently uploaded

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
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
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
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
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
 
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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
🐬 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
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
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
 

Recently uploaded (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
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
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
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
 
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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
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
 

Qt State Machine Framework

  • 1. Qt State Machine Framework 09/25/09
  • 2. About Me (Kent Hansen) • Working on Qt since 2005 • QtScript • Qt State Machine framework • Plays harmonica and Irish whistle 2
  • 3. Goals For This Talk • Introduce the Qt State Machine Framework (SMF) • Show how to incorporate it in your Qt application • Inspire you to think about how you would use it 3
  • 4. Agenda • State machines – what and why? • Statecharts tour • Qt State Machine tour • Wrap-up 4
  • 5. Qt State Machine Framework (SMF) • Introduced in Qt 4.6 • Part of QtCore module (ubiquitous) • Originated from Qt-SCXML research project 5
  • 6. Qt State Machine Framework (SMF) • Provides C++ API for creating hierarchical finite state machines (HFSMs) • Provides “interpreter” for executing HFSMs 6
  • 7. State Chart XML (SCXML) • “A general-purpose event-based state machine language” • W3C draft (http://www.w3.org/TR/scxml/) –Defines tags and attributes –Defines algorithm for interpretation 7
  • 8. Statecharts – Some use cases • State-based (“fluid”) UIs • Asynchronous communication • AI • Gesture recognition • Controller of Model-View-Controller • Your new, fancy product (e.g. “Hot dog oven”) 8
  • 10. Why State Machines in Qt? (I) ? 10
  • 11. Why State Machines in Qt? (II) • Program = Structure + Behavior • C++: Structure is language construct • C++: Event-driven behavior is not language construct 11
  • 12. Why State Machines in Qt? (III) • Qt already has event-based infrastructure • Event representation (QEvent) • Event dispatch • Event handlers • So what's the problem? 12
  • 13. 13
  • 14. The spaghetti code incident (I) “On button clicked: if X, do this else, do that” 14
  • 15. The spaghetti code incident (II) “On button clicked: if X, do this else, if Y or Z if I and not J do that else, do that other thing else, go and have a nap” 15
  • 16. ifs can get iffy; whiles can get wiley • if-statements --> state is implicit • Control flow and useful work jumbled together • Hard to understand and maintain • Hard to extend 16
  • 17. Can we do better...? 17
  • 18. Qt SMF Mission It shouldn't be your job to implement a general- purpose HFSM framework! 18
  • 20. … and there's control 20
  • 21. What's in it for YOU? • Write more robust code • Have your design and implementation speak the same language • Cope with incremental complexity 21
  • 22. Qt + State Machines = Very Good Fit • Natural extension to Qt's application model • Integration with meta-object system • Nested states fit nicely with Qt ownership model 22
  • 23. The right tool for the job... 23
  • 24. When NOT to use Qt SMF? • Lexical analysis, parsing, image decoding • When performance is critical • When abstraction level becomes too low • Not everything should be implemented as a (Qt) state machine! 24
  • 25. Agenda • State machines – what and why? • Statecharts overview • Qt State Machine tour • Wrap-up 25
  • 26. Statecharts overview • Composite (nested) states • Behavorial inheritance • History states 26
  • 28. A composite state decomposed 28
  • 29. “Get ready” state decomposed 29
  • 31. Composite states • “Zoom in”: Consider more details • “Zoom out”: Abstract away • Black box vs white box 31
  • 32. Like Father, Like Son... 32
  • 33. Behavioral Inheritance • States implicitly “inherit” the transitions of their ancestor state(s) • Enables grouping and specialization • Analogue: Class-based inheritance 33
  • 37. History states • Provide “pause and resume” functionality • State machine remembers active state • State machine restores last active state 37
  • 38. History state example from real life 38
  • 40. Agenda • State machines – what and why? • Statecharts overview • Qt State Machine tour • Wrap-up 40
  • 41. Qt State Machine tour • API introduction w/ small example • Events and transitions • Working with states • Using state machines in your application 41
  • 42. Qt State Machine API • Classes for representing states • Classes for representing transitions • Classes for state machine-specific events • QStateMachine class (container & interpreter) 42
  • 43. My First State Machine 43
  • 44. My First State Machine “Show me the code!” 44
  • 45. State machine set-up recipe • Create QStateMachine • Create states • Create transitions between states • Hook up to state signals (entered(), finished()) • Set the initial state • Start the machine 45
  • 46. State machine event processing • State machine runs its own event loop • QEvent-based • Use QStateMachine::postEvent() to post an event 46
  • 47. Transitions (I) • Abstract base class: QAbstractTransition –bool eventTest(QEvent*); • Has zero or more target states • Add to source state using QState::addTransition() 47
  • 48. Transitions (II) • Convenience for Qt signal transitions: addTransition(object, signal, targetState) • Standard Qt event (e.g. QMouseEvent) transitions also supported – QEventTransition 48
  • 49. Responding to state changes • QAbstractState::entered() signal • QAbstractState::exited() signal • QAbstractTransition::triggered() signal • QState::finished() signal 49
  • 50. Composite states • Follows Qt object hierarchy • Pass parent state to state constructor QState *s1 = new QState(); QState *s11 = new QState(s1); QState *s12 = new QState(s1); QFinalState *s13 = new QFinalState(s1); 50
  • 51. Paralell state group • Set the state's childMode property QState *s1 = new QState(); s1->setChildMode(QState::ParallelStates); QState *s11 = new QState(s1); QState *s12 = new QState(s1); 51
  • 52. History states • QHistoryState class • Create as child of composite state • Use the history state as target of a transition QState *s1 = new QState(); QHistoryState *s1h = new QHistoryState(s1); … s2->addTransition(foo, SIGNAL(bar()), s1h); 52
  • 53. How to use state machines...? 53
  • 54. Scenario: Game (I) • Many different types of game objects • Each type's behavior modeled as composite state • Events trigger transitions – Input (e.g. key press) • States operate on the game object –Setting properties (e.g. velocity) –Calling slots 54
  • 55. Scenario: Game (II) • Each game object has its own state machine • The machines run independently • Separate, top-level state machine that “orchestrates” –Game menus & modes –Start/quit 55
  • 56. Scenario: Game (III) • Presence of a state machine is encapsulated • Up to each type of object • “Simple” objects don't need to use a state machine 56
  • 57. States and animations • Integrates with Qt animation framework (also new in Qt 4.6) • QAbstractTransition::addAnimation() • Almost all Qt animation examples use Qt SMF 57
  • 58. My tips (I) • Use the meta-object system integration –assignProperty(object, propertyName, value) –entered() and exited() signals 58
  • 59. My tips (II) • Use composition –Build complex behavior from simple states –Take advantage of behavioral inheritance! –Don't subclass unnecessarily 59
  • 60. My tips (III) • Always draw the statechart first –Visualizing the design from C++ is hard –The statechart is the design document 60
  • 61. Agenda • State machines – what and why? • Statecharts tour • Qt State Machine tour • Wrap-up 61
  • 62. Summary (I) • Statecharts are a powerful tool for modeling complex, event-driven systems –General-purpose –Well-defined semantics 62
  • 63. Summary (II) • With the Qt State Machine Framework, you can build and run statecharts • Write more robust code • You need to consider when/where/how to use it 63
  • 64. The Future (Research) • Qt-SCXML to become part of Qt? • Qt state machine compiler • Visual design tool? • Your feedback matters! 64
  • 65. Relevant resources ● http://labs.qt.nokia.com ● http://lists.trolltech.com ● Qt Quarterly issue 30 ● irc.freenode.net: #qt-labs 65