SlideShare a Scribd company logo
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 Framework Events Signals Threads
Qt Framework Events Signals ThreadsQt Framework Events Signals Threads
Qt Framework Events Signals Threads
Neera Mital
 
Asp.net basic
Asp.net basicAsp.net basic
Asp.net basic
Neelesh Shukla
 
Improving app performance with Kotlin Coroutines
Improving app performance with Kotlin CoroutinesImproving app performance with Kotlin Coroutines
Improving app performance with Kotlin Coroutines
Hassan Abid
 
Qt multi threads
Qt multi threadsQt multi threads
Qt multi threads
Ynon Perek
 
.Net Overview -- Training (Lesson 1)
.Net Overview -- Training (Lesson 1).Net Overview -- Training (Lesson 1)
.Net Overview -- Training (Lesson 1)Rishi Kothari
 
JavaScript Event Loop
JavaScript Event LoopJavaScript Event Loop
JavaScript Event Loop
Designveloper
 
Introduction to Qt
Introduction to QtIntroduction to Qt
Introduction to Qt
Puja Pramudya
 
Basics of Model/View Qt programming
Basics of Model/View Qt programmingBasics of Model/View Qt programming
Basics of Model/View Qt programming
ICS
 
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
ICS
 
Rust-lang
Rust-langRust-lang
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#
Hawkman Academy
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
Tareq Hasan
 
Kotlin coroutines
Kotlin coroutinesKotlin coroutines
Kotlin coroutines
Robert Levonyan
 
C++ compilation process
C++ compilation processC++ compilation process
C++ compilation process
Rahul Jamwal
 
Introduction to SQLAlchemy ORM
Introduction to SQLAlchemy ORMIntroduction to SQLAlchemy ORM
Introduction to SQLAlchemy ORM
Jason Myers
 
Netbeans
NetbeansNetbeans
Netbeansacosdt
 
Netbeans IDE & Platform
Netbeans IDE & PlatformNetbeans IDE & Platform
Netbeans IDE & Platform
Aatul Palandurkar
 
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
ICS
 
LINQ in C#
LINQ in C#LINQ in C#
LINQ in C#
Basant Medhat
 

What's hot (20)

Qt Framework Events Signals Threads
Qt Framework Events Signals ThreadsQt Framework Events Signals Threads
Qt Framework Events Signals Threads
 
Asp.net basic
Asp.net basicAsp.net basic
Asp.net basic
 
Improving app performance with Kotlin Coroutines
Improving app performance with Kotlin CoroutinesImproving app performance with Kotlin Coroutines
Improving app performance with Kotlin Coroutines
 
Qt multi threads
Qt multi threadsQt multi threads
Qt multi threads
 
.Net Overview -- Training (Lesson 1)
.Net Overview -- Training (Lesson 1).Net Overview -- Training (Lesson 1)
.Net Overview -- Training (Lesson 1)
 
JavaScript Event Loop
JavaScript Event LoopJavaScript Event Loop
JavaScript Event Loop
 
C# basics
 C# basics C# basics
C# basics
 
Introduction to Qt
Introduction to QtIntroduction to Qt
Introduction to Qt
 
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 3
Best Practices in Qt Quick/QML - Part 3Best Practices in Qt Quick/QML - Part 3
Best Practices in Qt Quick/QML - Part 3
 
Rust-lang
Rust-langRust-lang
Rust-lang
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Kotlin coroutines
Kotlin coroutinesKotlin coroutines
Kotlin coroutines
 
C++ compilation process
C++ compilation processC++ compilation process
C++ compilation process
 
Introduction to SQLAlchemy ORM
Introduction to SQLAlchemy ORMIntroduction to SQLAlchemy ORM
Introduction to SQLAlchemy ORM
 
Netbeans
NetbeansNetbeans
Netbeans
 
Netbeans IDE & Platform
Netbeans IDE & PlatformNetbeans IDE & Platform
Netbeans IDE & Platform
 
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
 
LINQ in C#
LINQ in C#LINQ in C#
LINQ in C#
 

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 2
NokiaAppForum
 
Petri Niemi Qt Advanced Part 1
Petri Niemi Qt Advanced Part 1Petri Niemi Qt Advanced Part 1
Petri Niemi Qt Advanced Part 1
NokiaAppForum
 
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 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
Emertxe Information Technologies Pvt Ltd
 
Optimizing Performance in Qt-Based Applications
Optimizing Performance in Qt-Based ApplicationsOptimizing Performance in Qt-Based Applications
Optimizing Performance in Qt-Based Applications
account inactive
 
Intro to Kubernetes & GitOps Workshop
Intro to Kubernetes & GitOps WorkshopIntro to Kubernetes & GitOps Workshop
Intro to Kubernetes & GitOps Workshop
Weaveworks
 
Scripting Your Qt Application
Scripting Your Qt ApplicationScripting Your Qt Application
Scripting Your Qt Application
account inactive
 
QtQuick Day 1
QtQuick Day 1QtQuick Day 1
QtQuick Day 1
Timo Strömmer
 
HGZ Kaffeemaschinen & Qt Speak Coffee
HGZ Kaffeemaschinen & Qt Speak CoffeeHGZ Kaffeemaschinen & Qt Speak Coffee
HGZ Kaffeemaschinen & Qt Speak Coffee
account 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 UIs
account 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 Qt
account 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
 
Best Practices in Qt Quick/QML - Part I
Best Practices in Qt Quick/QML - Part IBest Practices in Qt Quick/QML - Part I
Best Practices in Qt Quick/QML - Part I
ICS
 
Quantum programming
Quantum programmingQuantum programming
Quantum programming
Francisco J. Gálvez Ramírez
 
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
account 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 & Gas
account 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 - Webinar
Janel 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
 
Best Practices in Qt Quick/QML - Part I
Best Practices in Qt Quick/QML - Part IBest Practices in Qt Quick/QML - Part I
Best Practices in Qt Quick/QML - Part I
 
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
 

More from account inactive

Meet Qt
Meet QtMeet Qt
KDE Plasma for Mobile Phones
KDE Plasma for Mobile PhonesKDE Plasma for Mobile Phones
KDE Plasma for Mobile Phones
account 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 Symbian
account inactive
 
The Future of Qt Widgets
The Future of Qt WidgetsThe Future of Qt Widgets
The Future of Qt Widgets
account inactive
 
Special Effects with Qt Graphics View
Special Effects with Qt Graphics ViewSpecial Effects with Qt Graphics View
Special Effects with Qt Graphics View
account inactive
 
Developments in The Qt WebKit Integration
Developments in The Qt WebKit IntegrationDevelopments in The Qt WebKit Integration
Developments in The Qt WebKit Integration
account inactive
 
Qt Kwan-Do
Qt Kwan-DoQt Kwan-Do
Qt Kwan-Do
account inactive
 
Qt on Real Time Operating Systems
Qt on Real Time Operating SystemsQt on Real Time Operating Systems
Qt on Real Time Operating Systems
account inactive
 
Development with Qt for Windows CE
Development with Qt for Windows CEDevelopment with Qt for Windows CE
Development with Qt for Windows CE
account inactive
 
Translating Qt Applications
Translating Qt ApplicationsTranslating Qt Applications
Translating Qt Applications
account inactive
 
Qt Creator Bootcamp
Qt Creator BootcampQt Creator Bootcamp
Qt Creator Bootcamp
account inactive
 
Qt Widget In-Depth
Qt Widget In-DepthQt Widget In-Depth
Qt Widget In-Depth
account inactive
 
Mobile Development with Qt for Symbian
Mobile Development with Qt for SymbianMobile Development with Qt for Symbian
Mobile Development with Qt for Symbian
account 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 Native
account 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 Mobility Project
The Mobility ProjectThe Mobility Project
The Mobility Project
account inactive
 
The Next Generation Qt Item Views
The Next Generation Qt Item ViewsThe Next Generation Qt Item Views
The Next Generation Qt Item Views
account inactive
 
Qt Licensing Explained
Qt Licensing ExplainedQt Licensing Explained
Qt Licensing Explained
account 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 Software
account 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 Processors
account 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

FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 

Recently uploaded (20)

FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 

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