SlideShare a Scribd company logo
1 of 10
Download to read offline
Qt 4.6
State Machine Framework
          By Roman Okolovich
What's New in Qt 4.6
   Animation Framework
   State Machine Framework
   Multi-touch & Gestures
   DOM access API
   Qt3D enablers
   Performance Optimizations
   Multimedia Audio Services
Link
Animation Framework
   The animation framework helps build highly animated, high-performance GUIs
    without the hassle of managing complex structures, timers, and easing curves,
    not to mention the large state graphs that all animated GUIs tend to be full of.
   The animation framework also plugs into the new Qt Statemachine by allowing an
    animation to be played when transitions are triggered. The state machine
    framework is introduced in 4.6 and is described below.
State Machine
   A state machine is a model of behavior
    composed of a number of states, transitions
    between those states, and actions. A state
    machine is an abstract model of a machine
    with a primitive internal memory.
   Wikipedia: Finite-state machine
State Machine Framework
   The state machine framework provides a robust state chart implementation based
    on Harel statecharts and SCXML. Qt's API lets you construct such state graphs
    and execute them. The key benefits of a state machine are:
       Simplify complex application semantics.
       Use of states to reduce code bloat.
       Use states to improve maintainability.
       Makes event-driven programming robust and more reusable.
   It is especially the last item here that makes using a state machine worthwhile. A
    key characteristic of event-driven systems (such as Qt applications) is that
    behavior often depends not only on the last or current event, but also the events
    that preceded it. With statecharts, this information is easy to express.
   The framework fits neatly into Qt by allowing transitions to trigger on signals and
    QEvents. By inserting animations into the state machine, it is also easier to use
    the framework for animating GUIs, for instance.
Classes in the State Machine Framework
    QAbstractState - The base class of states of a QStateMachine
    QAbstractTransition - The base class of transitions between QAbstractState
     objects
    QEventTransition - QObject-specific transition for Qt events
    QFinalState - Final state
    QHistoryState - Means of returning to a previously active substate
    QKeyEventTransition - Transition for key events
    QMouseEventTransition - Transition for mouse events
    QSignalEvent - Represents a Qt signal event
    QSignalTransition - Transition based on a Qt signal
    QState - General-purpose state for QStateMachine
    QStateMachine - Hierarchical finite state machine
    QWrappedEvent - Holds a clone of an event associated with a QObject
A Simple State Machine
      A state machine with three states, s1, s2 and s3. The state machine is controlled
       by a single QPushButton; when the button is clicked, the machine transitions to
       another state. Initially, the state machine is in state s1.


    First, we create the state machine and states:
         QStateMachine machine;
         QState *s1 = new QState();
         QState *s2 = new QState();
         QState *s3 = new QState();
    Then, we create the transitions by using
       the QState::addTransition() function:
         s1->addTransition(button, SIGNAL(clicked()), s2);
         s2->addTransition(button, SIGNAL(clicked()), s3);
         s3->addTransition(button, SIGNAL(clicked()), s1);
    Next, we add the states to the machine and set the machine's initial state:
         machine.addState(s1);
         machine.addState(s2);
         machine.addState(s3);
         machine.setInitialState(s1);
    Finally, we start the state machine:
         machine.start();

    The state machine executes asynchronously, i.e. it becomes part of your application's
      event loop.
Doing Useful Work on State Entry and Exit
   The QState::assignProperty() function can be used to have a state set a property
    of a QObject when the state is entered.
    s1->assignProperty(label, "text", "In state s1");
    s2->assignProperty(label, "text", "In state s2");
    s3->assignProperty(label, "text", "In state s3");

   When any of the states is entered, the label's text will be changed accordingly.
   The QState::entered() signal is emitted when the state is entered, and the
    QState::exited() signal is emitted when the state is exited.
    QObject::connect(s3, SIGNAL(entered()), button, SLOT(showMaximized()));
    QObject::connect(s3, SIGNAL(exited()), button, SLOT(showMinimized()));

   Custom states can reimplement QAbstractState::onEntry() and
    QAbstractState::onExit().
Animating Property Assignments
   The State Machine API connects with the Animation API in Qt to allow
    automatically animating properties as they are assigned in states.

QState *s1 = new QState();
QState *s2 = new QState();
s1->assignProperty(button, "geometry", QRectF(0, 0, 50, 50));
s2->assignProperty(button, "geometry", QRectF(0, 0, 100, 100));
s1->addTransition(button, SIGNAL(clicked()), s2);


   Here we define two states of a user interface. In s1 the button is small, and in s2
    it is bigger. If we click the button to transition from s1 to s2, the geometry of the
    button will be set immediately when a given state has been entered. If we want
    the transition to be smooth, however, all we need to do is make a
    QPropertyAnimation and add this to the transition object.

QSignalTransition *transition = s1->addTransition(button, SIGNAL(clicked()), s2);
transition->addAnimation(new QPropertyAnimation(button, "geometry"));
QPropertyAnimation.easingCurve
   This property holds the easing curve of the animation.
   This property defines the easing curve of the animation. By default, a linear
    easing curve is used, resulting in linear interpolation. Other curves are provided,
    for instance, QEasingCurve::InCirc, which provides a circular entry curve. Another
    example is QEasingCurve::InOutElastic, which provides an elastic effect on the
    values of the interpolated variant.

More Related Content

What's hot

Efficient Graphics with Qt
Efficient Graphics with QtEfficient Graphics with Qt
Efficient Graphics with Qt
Ariya Hidayat
 

What's hot (19)

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
 
Qt Widget In-Depth
Qt Widget In-DepthQt Widget In-Depth
Qt Widget In-Depth
 
Efficient Graphics with Qt
Efficient Graphics with QtEfficient Graphics with Qt
Efficient Graphics with Qt
 
Qt Graphics View Framework (Qt Developers Meetup Isreal)
Qt Graphics View Framework (Qt Developers Meetup Isreal)Qt Graphics View Framework (Qt Developers Meetup Isreal)
Qt Graphics View Framework (Qt Developers Meetup Isreal)
 
05 - Qt External Interaction and Graphics
05 - Qt External Interaction and Graphics05 - Qt External Interaction and Graphics
05 - Qt External Interaction and Graphics
 
Cross Platform Qt
Cross Platform QtCross Platform Qt
Cross Platform Qt
 
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
 
Qt on Real Time Operating Systems
Qt on Real Time Operating SystemsQt on Real Time Operating Systems
Qt on Real Time Operating Systems
 
Qt multi threads
Qt multi threadsQt multi threads
Qt multi threads
 
The Next Generation Qt Item Views
The Next Generation Qt Item ViewsThe Next Generation Qt Item Views
The Next Generation Qt Item Views
 
QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong? QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong?
 
The Future of Qt Widgets
The Future of Qt WidgetsThe Future of Qt Widgets
The Future of Qt Widgets
 
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
 
Qt
QtQt
Qt
 
A Brief Introduction to the Qt Application Framework
A Brief Introduction to the Qt Application FrameworkA Brief Introduction to the Qt Application Framework
A Brief Introduction to the Qt Application Framework
 
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
 
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
 
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
 
Qt for Beginners Part 3 - QML and Qt Quick
Qt for Beginners Part 3 - QML and Qt QuickQt for Beginners Part 3 - QML and Qt Quick
Qt for Beginners Part 3 - QML and Qt Quick
 

Similar to State Machine Framework

Kct guidev3p0
Kct guidev3p0Kct guidev3p0
Kct guidev3p0
aanchal19
 
Unity3D Scripting: State Machine
Unity3D Scripting: State MachineUnity3D Scripting: State Machine
Unity3D Scripting: State Machine
Sperasoft
 

Similar to State Machine Framework (20)

Qt Animation
Qt AnimationQt Animation
Qt Animation
 
Introduction to the Qt State Machine Framework using Qt 6
Introduction to the Qt State Machine Framework using Qt 6Introduction to the Qt State Machine Framework using Qt 6
Introduction to the Qt State Machine Framework using Qt 6
 
Kct guidev3p0
Kct guidev3p0Kct guidev3p0
Kct guidev3p0
 
Unity3D Scripting: State Machine
Unity3D Scripting: State MachineUnity3D Scripting: State Machine
Unity3D Scripting: State Machine
 
Sessionizing Uber Trips in Realtime - Flink Forward '18, Berlin
Sessionizing Uber Trips in Realtime  - Flink Forward '18, BerlinSessionizing Uber Trips in Realtime  - Flink Forward '18, Berlin
Sessionizing Uber Trips in Realtime - Flink Forward '18, Berlin
 
Flink Forward Berlin 2018: Amey Chaugule - "Threading Needles in a Haystack: ...
Flink Forward Berlin 2018: Amey Chaugule - "Threading Needles in a Haystack: ...Flink Forward Berlin 2018: Amey Chaugule - "Threading Needles in a Haystack: ...
Flink Forward Berlin 2018: Amey Chaugule - "Threading Needles in a Haystack: ...
 
Introduction to State Machines
Introduction to State MachinesIntroduction to State Machines
Introduction to State Machines
 
New Features in QP5
New Features in QP5New Features in QP5
New Features in QP5
 
Jar chapter 5_part_ii
Jar chapter 5_part_iiJar chapter 5_part_ii
Jar chapter 5_part_ii
 
Act as state machine
Act as state machineAct as state machine
Act as state machine
 
Flutter State Management Using GetX.pdf
Flutter State Management Using GetX.pdfFlutter State Management Using GetX.pdf
Flutter State Management Using GetX.pdf
 
Qt Workshop
Qt WorkshopQt Workshop
Qt Workshop
 
MCRL2
MCRL2MCRL2
MCRL2
 
Mcrl2 by kashif.namal@gmail.com, adnanskyousafzai@gmail.com
Mcrl2 by kashif.namal@gmail.com, adnanskyousafzai@gmail.comMcrl2 by kashif.namal@gmail.com, adnanskyousafzai@gmail.com
Mcrl2 by kashif.namal@gmail.com, adnanskyousafzai@gmail.com
 
Seminar State Chart1
Seminar State Chart1Seminar State Chart1
Seminar State Chart1
 
Pointer Events Working Group update / TPAC 2023 / Patrick H. Lauke
Pointer Events Working Group update / TPAC 2023 / Patrick H. LaukePointer Events Working Group update / TPAC 2023 / Patrick H. Lauke
Pointer Events Working Group update / TPAC 2023 / Patrick H. Lauke
 
Programming models for event controlled programs
Programming models for event controlled programsProgramming models for event controlled programs
Programming models for event controlled programs
 
The Ring programming language version 1.9 book - Part 93 of 210
The Ring programming language version 1.9 book - Part 93 of 210The Ring programming language version 1.9 book - Part 93 of 210
The Ring programming language version 1.9 book - Part 93 of 210
 
Declarative presentations UIKonf
Declarative presentations UIKonfDeclarative presentations UIKonf
Declarative presentations UIKonf
 
Ch2 finite automaton
Ch2 finite automatonCh2 finite automaton
Ch2 finite automaton
 

More from Roman Okolovich

Visual Studio 2008 Overview
Visual Studio 2008 OverviewVisual Studio 2008 Overview
Visual Studio 2008 Overview
Roman Okolovich
 

More from Roman Okolovich (11)

Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
 
C# XML documentation
C# XML documentationC# XML documentation
C# XML documentation
 
code analysis for c++
code analysis for c++code analysis for c++
code analysis for c++
 
Using QString effectively
Using QString effectivelyUsing QString effectively
Using QString effectively
 
Ram Disk
Ram DiskRam Disk
Ram Disk
 
64 bits for developers
64 bits for developers64 bits for developers
64 bits for developers
 
Virtual Functions
Virtual FunctionsVirtual Functions
Virtual Functions
 
Visual Studio 2008 Overview
Visual Studio 2008 OverviewVisual Studio 2008 Overview
Visual Studio 2008 Overview
 
The Big Three
The Big ThreeThe Big Three
The Big Three
 
Parallel Programming
Parallel ProgrammingParallel Programming
Parallel Programming
 
Smart Pointers
Smart PointersSmart Pointers
Smart Pointers
 

Recently uploaded

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
giselly40
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Recently uploaded (20)

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
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
[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
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
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...
 
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
 
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
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 

State Machine Framework

  • 1. Qt 4.6 State Machine Framework By Roman Okolovich
  • 2. What's New in Qt 4.6  Animation Framework  State Machine Framework  Multi-touch & Gestures  DOM access API  Qt3D enablers  Performance Optimizations  Multimedia Audio Services Link
  • 3. Animation Framework  The animation framework helps build highly animated, high-performance GUIs without the hassle of managing complex structures, timers, and easing curves, not to mention the large state graphs that all animated GUIs tend to be full of.  The animation framework also plugs into the new Qt Statemachine by allowing an animation to be played when transitions are triggered. The state machine framework is introduced in 4.6 and is described below.
  • 4. State Machine  A state machine is a model of behavior composed of a number of states, transitions between those states, and actions. A state machine is an abstract model of a machine with a primitive internal memory.  Wikipedia: Finite-state machine
  • 5. State Machine Framework  The state machine framework provides a robust state chart implementation based on Harel statecharts and SCXML. Qt's API lets you construct such state graphs and execute them. The key benefits of a state machine are:  Simplify complex application semantics.  Use of states to reduce code bloat.  Use states to improve maintainability.  Makes event-driven programming robust and more reusable.  It is especially the last item here that makes using a state machine worthwhile. A key characteristic of event-driven systems (such as Qt applications) is that behavior often depends not only on the last or current event, but also the events that preceded it. With statecharts, this information is easy to express.  The framework fits neatly into Qt by allowing transitions to trigger on signals and QEvents. By inserting animations into the state machine, it is also easier to use the framework for animating GUIs, for instance.
  • 6. Classes in the State Machine Framework  QAbstractState - The base class of states of a QStateMachine  QAbstractTransition - The base class of transitions between QAbstractState objects  QEventTransition - QObject-specific transition for Qt events  QFinalState - Final state  QHistoryState - Means of returning to a previously active substate  QKeyEventTransition - Transition for key events  QMouseEventTransition - Transition for mouse events  QSignalEvent - Represents a Qt signal event  QSignalTransition - Transition based on a Qt signal  QState - General-purpose state for QStateMachine  QStateMachine - Hierarchical finite state machine  QWrappedEvent - Holds a clone of an event associated with a QObject
  • 7. A Simple State Machine  A state machine with three states, s1, s2 and s3. The state machine is controlled by a single QPushButton; when the button is clicked, the machine transitions to another state. Initially, the state machine is in state s1. First, we create the state machine and states: QStateMachine machine; QState *s1 = new QState(); QState *s2 = new QState(); QState *s3 = new QState(); Then, we create the transitions by using the QState::addTransition() function: s1->addTransition(button, SIGNAL(clicked()), s2); s2->addTransition(button, SIGNAL(clicked()), s3); s3->addTransition(button, SIGNAL(clicked()), s1); Next, we add the states to the machine and set the machine's initial state: machine.addState(s1); machine.addState(s2); machine.addState(s3); machine.setInitialState(s1); Finally, we start the state machine: machine.start(); The state machine executes asynchronously, i.e. it becomes part of your application's event loop.
  • 8. Doing Useful Work on State Entry and Exit  The QState::assignProperty() function can be used to have a state set a property of a QObject when the state is entered. s1->assignProperty(label, "text", "In state s1"); s2->assignProperty(label, "text", "In state s2"); s3->assignProperty(label, "text", "In state s3");  When any of the states is entered, the label's text will be changed accordingly.  The QState::entered() signal is emitted when the state is entered, and the QState::exited() signal is emitted when the state is exited. QObject::connect(s3, SIGNAL(entered()), button, SLOT(showMaximized())); QObject::connect(s3, SIGNAL(exited()), button, SLOT(showMinimized()));  Custom states can reimplement QAbstractState::onEntry() and QAbstractState::onExit().
  • 9. Animating Property Assignments  The State Machine API connects with the Animation API in Qt to allow automatically animating properties as they are assigned in states. QState *s1 = new QState(); QState *s2 = new QState(); s1->assignProperty(button, "geometry", QRectF(0, 0, 50, 50)); s2->assignProperty(button, "geometry", QRectF(0, 0, 100, 100)); s1->addTransition(button, SIGNAL(clicked()), s2);  Here we define two states of a user interface. In s1 the button is small, and in s2 it is bigger. If we click the button to transition from s1 to s2, the geometry of the button will be set immediately when a given state has been entered. If we want the transition to be smooth, however, all we need to do is make a QPropertyAnimation and add this to the transition object. QSignalTransition *transition = s1->addTransition(button, SIGNAL(clicked()), s2); transition->addAnimation(new QPropertyAnimation(button, "geometry"));
  • 10. QPropertyAnimation.easingCurve  This property holds the easing curve of the animation.  This property defines the easing curve of the animation. By default, a linear easing curve is used, resulting in linear interpolation. Other curves are provided, for instance, QEasingCurve::InCirc, which provides a circular entry curve. Another example is QEasingCurve::InOutElastic, which provides an elastic effect on the values of the interpolated variant.