SlideShare une entreprise Scribd logo
1  sur  66
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

Qt Technical Presentation
Qt Technical PresentationQt Technical Presentation
Qt Technical PresentationDaniel Rocha
 
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
 
QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong? QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong? ICS
 
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
 
In-Depth Model/View with QML
In-Depth Model/View with QMLIn-Depth Model/View with QML
In-Depth Model/View with QMLICS
 
超簡単! JettyをWindowsにインストール
超簡単! JettyをWindowsにインストール超簡単! JettyをWindowsにインストール
超簡単! JettyをWindowsにインストールShin Tanigawa
 
Kotlin/Native 「使ってみた」の一歩先へ
Kotlin/Native 「使ってみた」の一歩先へKotlin/Native 「使ってみた」の一歩先へ
Kotlin/Native 「使ってみた」の一歩先へTakaki Hoshikawa
 
[Flutter] 來體驗 bloc 小方塊的神奇魔法 @Devfest 2022
[Flutter] 來體驗 bloc 小方塊的神奇魔法 @Devfest 2022[Flutter] 來體驗 bloc 小方塊的神奇魔法 @Devfest 2022
[Flutter] 來體驗 bloc 小方塊的神奇魔法 @Devfest 2022Johnny Sung
 
The Kotlin Programming Language
The Kotlin Programming LanguageThe Kotlin Programming Language
The Kotlin Programming Languageintelliyole
 
Overview of ZeroMQ
Overview of ZeroMQOverview of ZeroMQ
Overview of ZeroMQpieterh
 
Best Practices in Qt Quick/QML - Part 1 of 4
Best Practices in Qt Quick/QML - Part 1 of 4Best Practices in Qt Quick/QML - Part 1 of 4
Best Practices in Qt Quick/QML - Part 1 of 4ICS
 
計概:Programming Paradigm
計概:Programming Paradigm計概:Programming Paradigm
計概:Programming ParadigmRex Yuan
 
ライブラリ作成のすゝめ - 事例から見る個人OSS開発の効能
ライブラリ作成のすゝめ - 事例から見る個人OSS開発の効能ライブラリ作成のすゝめ - 事例から見る個人OSS開発の効能
ライブラリ作成のすゝめ - 事例から見る個人OSS開発の効能Yoshifumi Kawai
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first stepsRenato Primavera
 
Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in AngularKnoldus Inc.
 
Mocking in Java with Mockito
Mocking in Java with MockitoMocking in Java with Mockito
Mocking in Java with MockitoRichard Paul
 
ClassLoader Leak Patterns
ClassLoader Leak PatternsClassLoader Leak Patterns
ClassLoader Leak Patternsnekop
 

Tendances (20)

Qt Application Programming with C++ - Part 1
Qt Application Programming with C++ - Part 1Qt Application Programming with C++ - Part 1
Qt Application Programming with C++ - Part 1
 
Qt Technical Presentation
Qt Technical PresentationQt Technical Presentation
Qt Technical Presentation
 
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
 
QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong? QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong?
 
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
 
In-Depth Model/View with QML
In-Depth Model/View with QMLIn-Depth Model/View with QML
In-Depth Model/View with QML
 
超簡単! JettyをWindowsにインストール
超簡単! JettyをWindowsにインストール超簡単! JettyをWindowsにインストール
超簡単! JettyをWindowsにインストール
 
Kotlin/Native 「使ってみた」の一歩先へ
Kotlin/Native 「使ってみた」の一歩先へKotlin/Native 「使ってみた」の一歩先へ
Kotlin/Native 「使ってみた」の一歩先へ
 
[Flutter] 來體驗 bloc 小方塊的神奇魔法 @Devfest 2022
[Flutter] 來體驗 bloc 小方塊的神奇魔法 @Devfest 2022[Flutter] 來體驗 bloc 小方塊的神奇魔法 @Devfest 2022
[Flutter] 來體驗 bloc 小方塊的神奇魔法 @Devfest 2022
 
The Kotlin Programming Language
The Kotlin Programming LanguageThe Kotlin Programming Language
The Kotlin Programming Language
 
Overview of ZeroMQ
Overview of ZeroMQOverview of ZeroMQ
Overview of ZeroMQ
 
Smart pointers
Smart pointersSmart pointers
Smart pointers
 
Best Practices in Qt Quick/QML - Part 1 of 4
Best Practices in Qt Quick/QML - Part 1 of 4Best Practices in Qt Quick/QML - Part 1 of 4
Best Practices in Qt Quick/QML - Part 1 of 4
 
計概:Programming Paradigm
計概:Programming Paradigm計概:Programming Paradigm
計概:Programming Paradigm
 
ライブラリ作成のすゝめ - 事例から見る個人OSS開発の効能
ライブラリ作成のすゝめ - 事例から見る個人OSS開発の効能ライブラリ作成のすゝめ - 事例から見る個人OSS開発の効能
ライブラリ作成のすゝめ - 事例から見る個人OSS開発の効能
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in Angular
 
Mocking in Java with Mockito
Mocking in Java with MockitoMocking in Java with Mockito
Mocking in Java with Mockito
 
ClassLoader Leak Patterns
ClassLoader Leak PatternsClassLoader Leak Patterns
ClassLoader Leak Patterns
 
Qt for beginners
Qt for beginnersQt for beginners
Qt for beginners
 

Similaire à 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
 

Similaire à 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
 

Plus de 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
 

Plus de 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
 

Dernier

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
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
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
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
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
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
 
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
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
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
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 

Dernier (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
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...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
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...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
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
 
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
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 

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