SlideShare uma empresa Scribd logo
1 de 66
Baixar para ler offline
Qt State Machine Framework
                             09/25/09
About Me (Kent Hansen)

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




                                      2
Goals For This Talk

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




                                                      3
Agenda

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




                                   4
Qt State Machine Framework (SMF)

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




                                              5
Qt State Machine Framework (SMF)

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




                                                      6
State Chart XML (SCXML)

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




                                                 7
Statecharts – Some use cases

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



                                                  8
Mmm, hot dogs...




                   9
Why State Machines in Qt? (I)




                ?

                                10
Why State Machines in Qt? (II)

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




                                               11
Why State Machines in Qt? (III)

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




                                              12
13
The spaghetti code incident (I)

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




                                  14
The spaghetti code incident (II)

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




                                   15
ifs can get iffy; whiles can get wiley

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




                                                  16
Can we do better...?




                       17
Qt SMF Mission




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




                                                    18
There's flow...




                  19
… and there's control




                        20
What's in it for YOU?

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




                                                  21
Qt + State Machines = Very Good Fit

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




                                                     22
The right tool for the job...




                                23
When NOT to use Qt SMF?

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




                                                   24
Agenda

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




                                   25
Statecharts overview

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




                              26
A composite state




                    27
A composite state decomposed




                               28
“Get ready” state decomposed




                               29
“Speak” state decomposed




                           30
Composite states

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




                                     31
Like Father, Like Son...




                           32
Behavioral Inheritance

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




                                                         33
Behavioral Inheritance example (I)




                                     34
Behavioral Inheritance example (II)




                                      35
History matters...




                     36
History states

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




                                             37
History state example from real life




                                       38
History state example




                        39
Agenda

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




                                   40
Qt State Machine tour

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




                                             41
Qt State Machine API

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




                                                  42
My First State Machine




                         43
My First State Machine




            “Show me the code!”




                                  44
State machine set-up recipe

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



                                                     45
State machine event processing

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




                                                    46
Transitions (I)

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




                                                      47
Transitions (II)

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




                                                     48
Responding to state changes

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




                                            49
Composite states

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


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




                                           50
Paralell state group

• Set the state's childMode property



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




                                             51
History states

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

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


                                                    52
How to use state machines...?




                                53
Scenario: Game (I)

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




                                                    54
Scenario: Game (II)

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




                                               55
Scenario: Game (III)

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




                                                       56
States and animations

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




                                                     57
My tips (I)

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




                                                  58
My tips (II)

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




                                                59
My tips (III)

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




                                              60
Agenda

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




                                   61
Summary (I)

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




                                                 62
Summary (II)

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




                                                  63
The Future (Research)

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




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




                                 65
Thank You!

Questions?




             66

Mais conteúdo relacionado

Mais procurados

Linuxカーネルから紐解くAndroid
Linuxカーネルから紐解くAndroidLinuxカーネルから紐解くAndroid
Linuxカーネルから紐解くAndroiddemuyan
 
FPGAアクセラレータの作り方
FPGAアクセラレータの作り方FPGAアクセラレータの作り方
FPGAアクセラレータの作り方Mr. Vengineer
 
Best Practices in Qt Quick/QML - Part II
Best Practices in Qt Quick/QML - Part IIBest Practices in Qt Quick/QML - Part II
Best Practices in Qt Quick/QML - Part IIICS
 
ここがつらいよWebRTC - WebRTC開発の落とし穴
ここがつらいよWebRTC - WebRTC開発の落とし穴ここがつらいよWebRTC - WebRTC開発の落とし穴
ここがつらいよWebRTC - WebRTC開発の落とし穴mganeko
 
JJUG CCC 2018 Spring - I-7 (俺が)はじめての Netty
JJUG CCC 2018 Spring - I-7 (俺が)はじめての NettyJJUG CCC 2018 Spring - I-7 (俺が)はじめての Netty
JJUG CCC 2018 Spring - I-7 (俺が)はじめての NettyShinya Mochida
 
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 IICS
 
Interactive Music II - SuperCollider入門
Interactive Music II - SuperCollider入門Interactive Music II - SuperCollider入門
Interactive Music II - SuperCollider入門Atsushi Tadokoro
 
Qt Framework Events Signals Threads
Qt Framework Events Signals ThreadsQt Framework Events Signals Threads
Qt Framework Events Signals ThreadsNeera Mital
 
コルーチンでC++でも楽々ゲーム作成!
コルーチンでC++でも楽々ゲーム作成!コルーチンでC++でも楽々ゲーム作成!
コルーチンでC++でも楽々ゲーム作成!amusementcreators
 
C++ 프로젝트에 단위 테스트 도입하기
C++ 프로젝트에 단위 테스트 도입하기C++ 프로젝트에 단위 테스트 도입하기
C++ 프로젝트에 단위 테스트 도입하기OnGameServer
 
Android カスタムROMの作り方
Android カスタムROMの作り方Android カスタムROMの作り方
Android カスタムROMの作り方Masahiro Hidaka
 
Scripting Your Qt Application
Scripting Your Qt ApplicationScripting Your Qt Application
Scripting Your Qt Applicationaccount inactive
 
Observer pattern with Stl, boost and qt
Observer pattern with Stl, boost and qtObserver pattern with Stl, boost and qt
Observer pattern with Stl, boost and qtDaniel Eriksson
 
protothread and its usage in contiki OS
protothread and its usage in contiki OSprotothread and its usage in contiki OS
protothread and its usage in contiki OSSalah Amean
 
Qt Design Patterns
Qt Design PatternsQt Design Patterns
Qt Design PatternsYnon Perek
 
Qt and QML performance tips & tricks for Qt 4.7
Qt and QML performance tips & tricks for Qt 4.7Qt and QML performance tips & tricks for Qt 4.7
Qt and QML performance tips & tricks for Qt 4.7Pasi Kellokoski
 

Mais procurados (20)

Linuxカーネルから紐解くAndroid
Linuxカーネルから紐解くAndroidLinuxカーネルから紐解くAndroid
Linuxカーネルから紐解くAndroid
 
FPGAアクセラレータの作り方
FPGAアクセラレータの作り方FPGAアクセラレータの作り方
FPGAアクセラレータの作り方
 
Interpreter, Compiler, JIT from scratch
Interpreter, Compiler, JIT from scratchInterpreter, Compiler, JIT from scratch
Interpreter, Compiler, JIT from scratch
 
Best Practices in Qt Quick/QML - Part II
Best Practices in Qt Quick/QML - Part IIBest Practices in Qt Quick/QML - Part II
Best Practices in Qt Quick/QML - Part II
 
ここがつらいよWebRTC - WebRTC開発の落とし穴
ここがつらいよWebRTC - WebRTC開発の落とし穴ここがつらいよWebRTC - WebRTC開発の落とし穴
ここがつらいよWebRTC - WebRTC開発の落とし穴
 
JJUG CCC 2018 Spring - I-7 (俺が)はじめての Netty
JJUG CCC 2018 Spring - I-7 (俺が)はじめての NettyJJUG CCC 2018 Spring - I-7 (俺が)はじめての Netty
JJUG CCC 2018 Spring - I-7 (俺が)はじめての Netty
 
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
 
Interactive Music II - SuperCollider入門
Interactive Music II - SuperCollider入門Interactive Music II - SuperCollider入門
Interactive Music II - SuperCollider入門
 
Qt Framework Events Signals Threads
Qt Framework Events Signals ThreadsQt Framework Events Signals Threads
Qt Framework Events Signals Threads
 
Vim Rocks!
Vim Rocks!Vim Rocks!
Vim Rocks!
 
Qt Workshop
Qt WorkshopQt Workshop
Qt Workshop
 
コルーチンでC++でも楽々ゲーム作成!
コルーチンでC++でも楽々ゲーム作成!コルーチンでC++でも楽々ゲーム作成!
コルーチンでC++でも楽々ゲーム作成!
 
C++ 프로젝트에 단위 테스트 도입하기
C++ 프로젝트에 단위 테스트 도입하기C++ 프로젝트에 단위 테스트 도입하기
C++ 프로젝트에 단위 테스트 도입하기
 
Android カスタムROMの作り方
Android カスタムROMの作り方Android カスタムROMの作り方
Android カスタムROMの作り方
 
Scripting Your Qt Application
Scripting Your Qt ApplicationScripting Your Qt Application
Scripting Your Qt Application
 
Observer pattern with Stl, boost and qt
Observer pattern with Stl, boost and qtObserver pattern with Stl, boost and qt
Observer pattern with Stl, boost and qt
 
protothread and its usage in contiki OS
protothread and its usage in contiki OSprotothread and its usage in contiki OS
protothread and its usage in contiki OS
 
Qt Design Patterns
Qt Design PatternsQt Design Patterns
Qt Design Patterns
 
Virtual Machine Constructions for Dummies
Virtual Machine Constructions for DummiesVirtual Machine Constructions for Dummies
Virtual Machine Constructions for Dummies
 
Qt and QML performance tips & tricks for Qt 4.7
Qt and QML performance tips & tricks for Qt 4.7Qt and QML performance tips & tricks for Qt 4.7
Qt and QML performance tips & tricks for Qt 4.7
 

Semelhante a 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
 
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
 
The Kubernetes Operator Pattern - ContainerConf Nov 2017
The Kubernetes Operator Pattern - ContainerConf Nov 2017The Kubernetes Operator Pattern - ContainerConf Nov 2017
The Kubernetes Operator Pattern - ContainerConf Nov 2017Jakob Karalus
 

Semelhante a 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
 
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
 
The Kubernetes Operator Pattern - ContainerConf Nov 2017
The Kubernetes Operator Pattern - ContainerConf Nov 2017The Kubernetes Operator Pattern - ContainerConf Nov 2017
The Kubernetes Operator Pattern - ContainerConf Nov 2017
 

Mais 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
 

Mais 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
 

Último

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 

Último (20)

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.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