SlideShare a Scribd company logo
8
Most read
9
Most read
15
Most read
Integrated Computer Solutions Inc. www.ics.com
An Introduction to the Qt State
Machine Framework using Qt 6
December 16, 2021
Christopher Probst, ICS
1
Integrated Computer Solutions Inc. www.ics.com
About ICS
Delivering Smart Devices for a Connected World
โ— Founded in 1987
โ— Largest source of independent Qt expertise in North America
โ— Trusted Qt Service Partner since 2002
โ— Exclusive Open Enrollment Training Partner in North America
โ— Provides integrated custom software development and user experience (UX) design
โ— Embedded, touchscreen, mobile and desktop applications
โ— HQ in Waltham, MA with o๏ฌƒces in California, Canada, Europe
Boston UX
โ— Part of the ICS family, focusing on UX design
โ— Designs intuitive touchscreen interfaces for high-impact embedded
and connected medical, industrial and consumer devices
2
Integrated Computer Solutions Inc. www.ics.com
โ— A C++, (and also QML) API that allows to create and execute state graphs
โ— Not to be confused with the state and transitions from Item in QML
โ— Based on Harelโ€™s statecharts
โ— Allows for hierarchical statecharts with superstates
โ— State Machine is signal and event driven
โ— https://doc-snapshots.qt.io/qt6-dev/qtstatemachine-index.html
What is the Qt State Machine Framework
3
Integrated Computer Solutions Inc. www.ics.com
C++ Type Description
QStateMachine Provides a hierarchical ๏ฌnite state machine
QState Provides a state for the state machine
QHistoryState Provides a means of returning to a previously active sub-state
QFinalState Provides a ๏ฌnal state
QSignalTransition Provides a transition based on a Qt signal
QEventTransition Provides an a transition based on a QEvent
Note: To access the API it is necessary to link against the Qt module StateMachine:
C++ Types Available
find_package(Qt6 COMPONENTS StateMachine REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::StateMachine)
CMakeLists.txt
4
Integrated Computer Solutions Inc. www.ics.com
An Example
5
Integrated Computer Solutions Inc. www.ics.com
Business Logic/UI Paradigm
-
BusinessLogic
C++
UI in QML
โ— C++ code โ€œknowsโ€ nothing of the implementation details in QML
6
Integrated Computer Solutions Inc. www.ics.com
Using C++ and QML
7
7
Integrated Computer Solutions Inc. www.ics.com
Creating State Machine
8
Just create an instance of a QStateMachine in the business logic...
class ScreenSelector : public QObject
{
Q_OBJECT
Q_PROPERTY(QUrl currentScreen READ currentScreen WRITE setCurrentScreen NOTIFY currentScreenChanged)
public:
explicit ScreenSelector(QObject *parent = nullptr);
const QUrl &currentScreen() const;
void setCurrentScreen(const QUrl &newCurrentScreen);
signals:
void currentScreenChanged();
private:
QUrl m_currentScreen;
QStateMachine m_statemachine; //instantiating a QStateMachine object
};
Integrated Computer Solutions Inc. www.ics.com
Creating States
QState *loggedInstate = new QState();
loggedInstate->setObjectName("LoggedIn");
loggedInstate->assignProperty(this, "currentScreen",
QUrl("CoolApplication.qml"));
QState *loggedOutState = new QState();
loggedOutState->setObjectName("LoggedOut");
loggedOutState->assignProperty(this, "currentScreen",
QUrl("LoginScreen.qml"));
QState *screnSaverMode = new QState();
screnSaverMode->setObjectName("ScrenSaverMode");
screnSaverMode->assignProperty(this, "currentScreen",
QUrl("ScreenSaver.qml"));
9
โ— Just create an instance of a QState
in the business logic
โ— QState represents a set of properties
and values
โ— The entry into a state triggers the
value setting of properties as
speci๏ฌed with the method
QState::assignProperty(QObject *,
char *, const QVariant &)
Integrated Computer Solutions Inc. www.ics.com
Creating a Superstate
QState *working = new QState(); //This is a super State
working->setObjectName("working");
QState *loggedInstate = new QState(working);
loggedInstate->setObjectName("loggedIn");
loggedInstate->assignProperty(this, "currentScreen",
QUrl("CoolApplication.qml"));
QState *loggedOutState = new QState(working);
loggedOutState->setObjectName("loggedOut");
loggedOutState->assignProperty(this, "currentScreen",
QUrl("LoginScreen.qml"));
QHistoryState *workingh = new QHistoryState(working);
working->setInitialState(loggedInstate);
10
Just create an instance of a QState and use
the parent/child relationship to specify the
state hierarchy
Set an initial state of the superstate with
QState::setInitialState(QAbstractState)
Take advantage of QHistoryState to reference
its last active sub-state
Integrated Computer Solutions Inc. www.ics.com
Adding States to the State Machine
QState *working = new QState(); //This is a super State
...
QState *loggedInstate = new QState(working);
...
QState *loggedOutState = new QState(working);
...
QHistoryState *workingh = new QHistoryState(working);
QState *screnSaverMode = new QState();
...
//Adding States to State Machine
m_statemachine.addState( working );
m_statemachine.addState( screnSaverMode );
m_statemachine.setInitialState( loggedInstate) ;
m_statemachine.start()
โ— Use void
QStateMachine::addState(QAbstract
State *state)
โ— Or make the states children of the
state machine
โ— Set the initial using void
QState::setInitialState(QAbstract
State *state)
โ— Donโ€™t forget to start the machine
using void QStateMachine::start()
11
Integrated Computer Solutions Inc. www.ics.com
But What About the Transitions?
12
QState *loggedInstate = new QState(working);
...
QState *loggedOutState = new QState(working);
...
loggedOutState->addTransition(&m_communicationLayer,
&CommunicationLayer::loggedIn,
loggedInstate);
loggedInstate->addTransition(&m_communicationLayer,
&CommunicationLayer::loggedOut,
loggedOutState);
โ— Use QState::addTransition
โ— Can be used directly with instance of a QSignalTransition or QEventTransition
Integrated Computer Solutions Inc. www.ics.com
And What about the History and Super State?
13
QState *working = new QState(); //This is a super State
QHistoryState *workingh = new QHistoryState(working);
โ€ฆ
QState *screenSaverMode = new QState();
screenSaverMode->setObjectName("screnSaverMode");
screenSaverMode->assignProperty(this, "currentScreen",
QUrl("Screensaver.qml"));
working->addTransition(this, &ScreenSelector::inactiveForTooLong,
screenSaverMode);
screenSaverMode->addTransition(this, &ScreenSelector::wokenUp,
workingh);
โ— Still Use QState::addTransition
โ— The History state refers to the last active sub-state
Integrated Computer Solutions Inc. www.ics.com
Retrieving States from the State Machine
14
//Retrieving Logged Out State
QState* aState = m_statemachine.findChild<QState*>("loggedOut");
//Retrieving all States
QList<QState*> states =
m_statemachine.findChildren<QState*>( QRegularExpression(".*") );
//Retrieving Direct States
QList<QObject*> states = m_statemachine.children();
//Display current states of state machine upon entry of specialState
connect(*specialState, &QState::entered,
[this](){
qDebug() << m_statemachine.configuration();
}
);
โ— The QObject parent/child
relationship applies
โ— QStates are children of
QStateMachine
โ— The QObject findChild() and
findChildren() methods assist in
retrieving states
โ— QStateMachine::configuration() lists
the current states of a state
machine
Integrated Computer Solutions Inc. www.ics.com
Conditional Transitions
15
class ConditionalTransition : public QSignalTransition {
Q_OBJECT
Q_PROPERTY(bool canTrigger READ canTrigger WRITE setCanTrigger)
public:
ConditionalTransition(QState * sourceState = 0): QSignalTransition(sourceState)
{
m_canTrigger = false;
}
void setCanTrigger(bool v) { m_canTrigger = v; }
bool canTrigger() const { return m_canTrigger; }
protected:
bool eventTest(QEvent *e) {
if(!QSignalTransition::eventTest(e))
return false;
return canTrigger(); }
private:
bool m_canTrigger;
}
โ— Somewhat tricky
โ— Requires the re-implementation of
a transition class
โ— And the re-implementation of
bool eventTest(QEvent *event)
โ— Return true if the condition is met
Integrated Computer Solutions Inc. www.ics.com
Advantages of Using the State Machine Framework
16
โ— Allows for Graphical State Machine Designer Tooling.
Examples:
โ— Qt SCXML https://doc.qt.io/qt-5/qtscxml-overview.html,
โ— KDAB State machine Editor
https://github.com/KDAB/KDStateMachineEditor
โ— Would allow for analysis of the overall logic
โ— Allows for easy and precise documentation of requirements
Integrated Computer Solutions Inc. www.ics.com
Thank you!
17
Any questions?

More Related Content

What's hot (20)

PDF
Lessons Learned from Building 100+ C++/Qt/QML Devices
ICS
ย 
PDF
Best Practices in Qt Quick/QML - Part 4
ICS
ย 
PDF
Best Practices in Qt Quick/QML - Part II
ICS
ย 
PDF
Introduction to QML
Alan Uthoff
ย 
PPTX
Introduction to Qt
Puja Pramudya
ย 
PPTX
Qt Framework Events Signals Threads
Neera Mital
ย 
PDF
QThreads: Are You Using Them Wrong?
ICS
ย 
PDF
Qt Internationalization
ICS
ย 
PDF
Qt for Beginners Part 3 - QML and Qt Quick
ICS
ย 
PDF
Qt multi threads
Ynon Perek
ย 
PDF
Basics of Model/View Qt programming
ICS
ย 
PPTX
Qt Qml
Steven Song
ย 
PDF
Qt programming-using-cpp
Emertxe Information Technologies Pvt Ltd
ย 
PPTX
Qt for beginners part 1 overview and key concepts
ICS
ย 
PPTX
FULL stack -> MEAN stack
Ashok Raj
ย 
PDF
Qt Installer Framework
ICS
ย 
PDF
Introduction to the Qt Quick Scene Graph
ICS
ย 
PDF
Meet Qt 6.0
Qt
ย 
PDF
Qt Application Programming with C++ - Part 1
Emertxe Information Technologies Pvt Ltd
ย 
ODP
Qt Workshop
Johan Thelin
ย 
Lessons Learned from Building 100+ C++/Qt/QML Devices
ICS
ย 
Best Practices in Qt Quick/QML - Part 4
ICS
ย 
Best Practices in Qt Quick/QML - Part II
ICS
ย 
Introduction to QML
Alan Uthoff
ย 
Introduction to Qt
Puja Pramudya
ย 
Qt Framework Events Signals Threads
Neera Mital
ย 
QThreads: Are You Using Them Wrong?
ICS
ย 
Qt Internationalization
ICS
ย 
Qt for Beginners Part 3 - QML and Qt Quick
ICS
ย 
Qt multi threads
Ynon Perek
ย 
Basics of Model/View Qt programming
ICS
ย 
Qt Qml
Steven Song
ย 
Qt programming-using-cpp
Emertxe Information Technologies Pvt Ltd
ย 
Qt for beginners part 1 overview and key concepts
ICS
ย 
FULL stack -> MEAN stack
Ashok Raj
ย 
Qt Installer Framework
ICS
ย 
Introduction to the Qt Quick Scene Graph
ICS
ย 
Meet Qt 6.0
Qt
ย 
Qt Application Programming with C++ - Part 1
Emertxe Information Technologies Pvt Ltd
ย 
Qt Workshop
Johan Thelin
ย 

Similar to Introduction to the Qt State Machine Framework using Qt 6 (20)

PDF
State Machine Framework
Roman Okolovich
ย 
PDF
Qt State Machine Framework
account inactive
ย 
PDF
Petri Niemi Qt Advanced Part 2
NokiaAppForum
ย 
PDF
Best Practices in Qt Quick/QML - Part 2
Janel Heilbrunn
ย 
PDF
Best Practices in Qt Quick/QML - Part 2
ICS
ย 
PDF
Qt Animation
William Lee
ย 
PDF
Lockless Producer Consumer Threads: Asynchronous Communications Made Easy
ICS
ย 
PDF
Meet the Widgets: Another Way to Implement UI
ICS
ย 
PDF
Fun with QML
ICS
ย 
PDF
New Features in QP5
Quantum Leaps, LLC
ย 
PPT
C:\documents and settings\student\desktop\swaroop uml
satyaiswaroop
ย 
PDF
Qt Multiplatform development
Sergio Shevchenko
ย 
PPTX
Unit 4- State Machine in mobile programming
LeahRachael
ย 
PDF
Migrating from Photon to Qt
Janel Heilbrunn
ย 
PDF
Migrating from Photon to Qt
ICS
ย 
PDF
Qt for beginners part 4 doing more
ICS
ย 
PDF
Qt Automotive Suite - under the hood // Qt World Summit 2017
Johan Thelin
ย 
PDF
Qt Quick in depth
Develer S.r.l.
ย 
PPTX
Qt for beginners part 5 ask the experts
ICS
ย 
PDF
Qt Application Programming with C++ - Part 2
Emertxe Information Technologies Pvt Ltd
ย 
State Machine Framework
Roman Okolovich
ย 
Qt State Machine Framework
account inactive
ย 
Petri Niemi Qt Advanced Part 2
NokiaAppForum
ย 
Best Practices in Qt Quick/QML - Part 2
Janel Heilbrunn
ย 
Best Practices in Qt Quick/QML - Part 2
ICS
ย 
Qt Animation
William Lee
ย 
Lockless Producer Consumer Threads: Asynchronous Communications Made Easy
ICS
ย 
Meet the Widgets: Another Way to Implement UI
ICS
ย 
Fun with QML
ICS
ย 
New Features in QP5
Quantum Leaps, LLC
ย 
C:\documents and settings\student\desktop\swaroop uml
satyaiswaroop
ย 
Qt Multiplatform development
Sergio Shevchenko
ย 
Unit 4- State Machine in mobile programming
LeahRachael
ย 
Migrating from Photon to Qt
Janel Heilbrunn
ย 
Migrating from Photon to Qt
ICS
ย 
Qt for beginners part 4 doing more
ICS
ย 
Qt Automotive Suite - under the hood // Qt World Summit 2017
Johan Thelin
ย 
Qt Quick in depth
Develer S.r.l.
ย 
Qt for beginners part 5 ask the experts
ICS
ย 
Qt Application Programming with C++ - Part 2
Emertxe Information Technologies Pvt Ltd
ย 
Ad

More from ICS (20)

PDF
Porting Qt 5 QML Modules to Qt 6 Webinar
ICS
ย 
PDF
Medical Device Cybersecurity Threat & Risk Scoring
ICS
ย 
PDF
Exploring Wayland: A Modern Display Server for the Future
ICS
ย 
PDF
Threat Modeling & Risk Assessment Webinar: A Step-by-Step Example
ICS
ย 
PDF
8 Mandatory Security Control Categories for Successful Submissions
ICS
ย 
PDF
Future-Proofing Embedded Device Capabilities with the Qt 6 Plugin Mechanism.pdf
ICS
ย 
PDF
Choosing an Embedded GUI: Comparative Analysis of UI Frameworks
ICS
ย 
PDF
Medical Device Cyber Testing to Meet FDA Requirements
ICS
ย 
PDF
Threat Modeling and Risk Assessment Webinar.pdf
ICS
ย 
PDF
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
ICS
ย 
PDF
Webinar On-Demand: Using Flutter for Embedded
ICS
ย 
PDF
A Deep Dive into Secure Product Development Frameworks.pdf
ICS
ย 
PDF
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
ICS
ย 
PDF
Practical Advice for FDAโ€™s 510(k) Requirements.pdf
ICS
ย 
PDF
Accelerating Development of a Safety-Critical Cobot Welding System with Qt/QM...
ICS
ย 
PDF
Overcoming CMake Configuration Issues Webinar
ICS
ย 
PDF
Enhancing Quality and Test in Medical Device Design - Part 2.pdf
ICS
ย 
PDF
Designing and Managing IoT Devices for Rapid Deployment - Webinar.pdf
ICS
ย 
PDF
Quality and Test in Medical Device Design - Part 1.pdf
ICS
ย 
PDF
Creating Digital Twins Using Rapid Development Techniques.pdf
ICS
ย 
Porting Qt 5 QML Modules to Qt 6 Webinar
ICS
ย 
Medical Device Cybersecurity Threat & Risk Scoring
ICS
ย 
Exploring Wayland: A Modern Display Server for the Future
ICS
ย 
Threat Modeling & Risk Assessment Webinar: A Step-by-Step Example
ICS
ย 
8 Mandatory Security Control Categories for Successful Submissions
ICS
ย 
Future-Proofing Embedded Device Capabilities with the Qt 6 Plugin Mechanism.pdf
ICS
ย 
Choosing an Embedded GUI: Comparative Analysis of UI Frameworks
ICS
ย 
Medical Device Cyber Testing to Meet FDA Requirements
ICS
ย 
Threat Modeling and Risk Assessment Webinar.pdf
ICS
ย 
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
ICS
ย 
Webinar On-Demand: Using Flutter for Embedded
ICS
ย 
A Deep Dive into Secure Product Development Frameworks.pdf
ICS
ย 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
ICS
ย 
Practical Advice for FDAโ€™s 510(k) Requirements.pdf
ICS
ย 
Accelerating Development of a Safety-Critical Cobot Welding System with Qt/QM...
ICS
ย 
Overcoming CMake Configuration Issues Webinar
ICS
ย 
Enhancing Quality and Test in Medical Device Design - Part 2.pdf
ICS
ย 
Designing and Managing IoT Devices for Rapid Deployment - Webinar.pdf
ICS
ย 
Quality and Test in Medical Device Design - Part 1.pdf
ICS
ย 
Creating Digital Twins Using Rapid Development Techniques.pdf
ICS
ย 
Ad

Recently uploaded (20)

PPTX
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
ย 
PDF
WholeClear Split vCard Software for Split large vCard file
markwillsonmw004
ย 
PPTX
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
ย 
PDF
GridView,Recycler view, API, SQLITE& NetworkRequest.pdf
Nabin Dhakal
ย 
PPTX
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
ย 
PPTX
computer forensics encase emager app exp6 1.pptx
ssuser343e92
ย 
PDF
Code Once; Run Everywhere - A Beginnerโ€™s Journey with React Native
Hasitha Walpola
ย 
PPTX
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
ย 
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
ย 
PDF
65811_Introducing the Fusion AI Agent Studio (1).pdf
g6129590
ย 
PDF
>Wondershare Filmora Crack Free Download 2025
utfefguu
ย 
PDF
Laboratory Workflows Digitalized and live in 90 days with Scifeonยดs SAPPA P...
info969686
ย 
PPTX
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
ย 
PDF
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
ย 
PDF
LPS25 - Operationalizing MLOps in GEP - Terradue.pdf
terradue
ย 
PDF
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
ย 
PPTX
WYSIWYG Web Builder Crack 2025 โ€“ Free Download Full Version with License Key
HyperPc soft
ย 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
ย 
PPTX
CONCEPT OF PROGRAMMING in language .pptx
tamim41
ย 
PDF
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
ย 
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
ย 
WholeClear Split vCard Software for Split large vCard file
markwillsonmw004
ย 
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
ย 
GridView,Recycler view, API, SQLITE& NetworkRequest.pdf
Nabin Dhakal
ย 
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
ย 
computer forensics encase emager app exp6 1.pptx
ssuser343e92
ย 
Code Once; Run Everywhere - A Beginnerโ€™s Journey with React Native
Hasitha Walpola
ย 
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
ย 
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
ย 
65811_Introducing the Fusion AI Agent Studio (1).pdf
g6129590
ย 
>Wondershare Filmora Crack Free Download 2025
utfefguu
ย 
Laboratory Workflows Digitalized and live in 90 days with Scifeonยดs SAPPA P...
info969686
ย 
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
ย 
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
ย 
LPS25 - Operationalizing MLOps in GEP - Terradue.pdf
terradue
ย 
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
ย 
WYSIWYG Web Builder Crack 2025 โ€“ Free Download Full Version with License Key
HyperPc soft
ย 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
ย 
CONCEPT OF PROGRAMMING in language .pptx
tamim41
ย 
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
ย 

Introduction to the Qt State Machine Framework using Qt 6

  • 1. Integrated Computer Solutions Inc. www.ics.com An Introduction to the Qt State Machine Framework using Qt 6 December 16, 2021 Christopher Probst, ICS 1
  • 2. Integrated Computer Solutions Inc. www.ics.com About ICS Delivering Smart Devices for a Connected World โ— Founded in 1987 โ— Largest source of independent Qt expertise in North America โ— Trusted Qt Service Partner since 2002 โ— Exclusive Open Enrollment Training Partner in North America โ— Provides integrated custom software development and user experience (UX) design โ— Embedded, touchscreen, mobile and desktop applications โ— HQ in Waltham, MA with o๏ฌƒces in California, Canada, Europe Boston UX โ— Part of the ICS family, focusing on UX design โ— Designs intuitive touchscreen interfaces for high-impact embedded and connected medical, industrial and consumer devices 2
  • 3. Integrated Computer Solutions Inc. www.ics.com โ— A C++, (and also QML) API that allows to create and execute state graphs โ— Not to be confused with the state and transitions from Item in QML โ— Based on Harelโ€™s statecharts โ— Allows for hierarchical statecharts with superstates โ— State Machine is signal and event driven โ— https://doc-snapshots.qt.io/qt6-dev/qtstatemachine-index.html What is the Qt State Machine Framework 3
  • 4. Integrated Computer Solutions Inc. www.ics.com C++ Type Description QStateMachine Provides a hierarchical ๏ฌnite state machine QState Provides a state for the state machine QHistoryState Provides a means of returning to a previously active sub-state QFinalState Provides a ๏ฌnal state QSignalTransition Provides a transition based on a Qt signal QEventTransition Provides an a transition based on a QEvent Note: To access the API it is necessary to link against the Qt module StateMachine: C++ Types Available find_package(Qt6 COMPONENTS StateMachine REQUIRED) target_link_libraries(mytarget PRIVATE Qt6::StateMachine) CMakeLists.txt 4
  • 5. Integrated Computer Solutions Inc. www.ics.com An Example 5
  • 6. Integrated Computer Solutions Inc. www.ics.com Business Logic/UI Paradigm - BusinessLogic C++ UI in QML โ— C++ code โ€œknowsโ€ nothing of the implementation details in QML 6
  • 7. Integrated Computer Solutions Inc. www.ics.com Using C++ and QML 7 7
  • 8. Integrated Computer Solutions Inc. www.ics.com Creating State Machine 8 Just create an instance of a QStateMachine in the business logic... class ScreenSelector : public QObject { Q_OBJECT Q_PROPERTY(QUrl currentScreen READ currentScreen WRITE setCurrentScreen NOTIFY currentScreenChanged) public: explicit ScreenSelector(QObject *parent = nullptr); const QUrl &currentScreen() const; void setCurrentScreen(const QUrl &newCurrentScreen); signals: void currentScreenChanged(); private: QUrl m_currentScreen; QStateMachine m_statemachine; //instantiating a QStateMachine object };
  • 9. Integrated Computer Solutions Inc. www.ics.com Creating States QState *loggedInstate = new QState(); loggedInstate->setObjectName("LoggedIn"); loggedInstate->assignProperty(this, "currentScreen", QUrl("CoolApplication.qml")); QState *loggedOutState = new QState(); loggedOutState->setObjectName("LoggedOut"); loggedOutState->assignProperty(this, "currentScreen", QUrl("LoginScreen.qml")); QState *screnSaverMode = new QState(); screnSaverMode->setObjectName("ScrenSaverMode"); screnSaverMode->assignProperty(this, "currentScreen", QUrl("ScreenSaver.qml")); 9 โ— Just create an instance of a QState in the business logic โ— QState represents a set of properties and values โ— The entry into a state triggers the value setting of properties as speci๏ฌed with the method QState::assignProperty(QObject *, char *, const QVariant &)
  • 10. Integrated Computer Solutions Inc. www.ics.com Creating a Superstate QState *working = new QState(); //This is a super State working->setObjectName("working"); QState *loggedInstate = new QState(working); loggedInstate->setObjectName("loggedIn"); loggedInstate->assignProperty(this, "currentScreen", QUrl("CoolApplication.qml")); QState *loggedOutState = new QState(working); loggedOutState->setObjectName("loggedOut"); loggedOutState->assignProperty(this, "currentScreen", QUrl("LoginScreen.qml")); QHistoryState *workingh = new QHistoryState(working); working->setInitialState(loggedInstate); 10 Just create an instance of a QState and use the parent/child relationship to specify the state hierarchy Set an initial state of the superstate with QState::setInitialState(QAbstractState) Take advantage of QHistoryState to reference its last active sub-state
  • 11. Integrated Computer Solutions Inc. www.ics.com Adding States to the State Machine QState *working = new QState(); //This is a super State ... QState *loggedInstate = new QState(working); ... QState *loggedOutState = new QState(working); ... QHistoryState *workingh = new QHistoryState(working); QState *screnSaverMode = new QState(); ... //Adding States to State Machine m_statemachine.addState( working ); m_statemachine.addState( screnSaverMode ); m_statemachine.setInitialState( loggedInstate) ; m_statemachine.start() โ— Use void QStateMachine::addState(QAbstract State *state) โ— Or make the states children of the state machine โ— Set the initial using void QState::setInitialState(QAbstract State *state) โ— Donโ€™t forget to start the machine using void QStateMachine::start() 11
  • 12. Integrated Computer Solutions Inc. www.ics.com But What About the Transitions? 12 QState *loggedInstate = new QState(working); ... QState *loggedOutState = new QState(working); ... loggedOutState->addTransition(&m_communicationLayer, &CommunicationLayer::loggedIn, loggedInstate); loggedInstate->addTransition(&m_communicationLayer, &CommunicationLayer::loggedOut, loggedOutState); โ— Use QState::addTransition โ— Can be used directly with instance of a QSignalTransition or QEventTransition
  • 13. Integrated Computer Solutions Inc. www.ics.com And What about the History and Super State? 13 QState *working = new QState(); //This is a super State QHistoryState *workingh = new QHistoryState(working); โ€ฆ QState *screenSaverMode = new QState(); screenSaverMode->setObjectName("screnSaverMode"); screenSaverMode->assignProperty(this, "currentScreen", QUrl("Screensaver.qml")); working->addTransition(this, &ScreenSelector::inactiveForTooLong, screenSaverMode); screenSaverMode->addTransition(this, &ScreenSelector::wokenUp, workingh); โ— Still Use QState::addTransition โ— The History state refers to the last active sub-state
  • 14. Integrated Computer Solutions Inc. www.ics.com Retrieving States from the State Machine 14 //Retrieving Logged Out State QState* aState = m_statemachine.findChild<QState*>("loggedOut"); //Retrieving all States QList<QState*> states = m_statemachine.findChildren<QState*>( QRegularExpression(".*") ); //Retrieving Direct States QList<QObject*> states = m_statemachine.children(); //Display current states of state machine upon entry of specialState connect(*specialState, &QState::entered, [this](){ qDebug() << m_statemachine.configuration(); } ); โ— The QObject parent/child relationship applies โ— QStates are children of QStateMachine โ— The QObject findChild() and findChildren() methods assist in retrieving states โ— QStateMachine::configuration() lists the current states of a state machine
  • 15. Integrated Computer Solutions Inc. www.ics.com Conditional Transitions 15 class ConditionalTransition : public QSignalTransition { Q_OBJECT Q_PROPERTY(bool canTrigger READ canTrigger WRITE setCanTrigger) public: ConditionalTransition(QState * sourceState = 0): QSignalTransition(sourceState) { m_canTrigger = false; } void setCanTrigger(bool v) { m_canTrigger = v; } bool canTrigger() const { return m_canTrigger; } protected: bool eventTest(QEvent *e) { if(!QSignalTransition::eventTest(e)) return false; return canTrigger(); } private: bool m_canTrigger; } โ— Somewhat tricky โ— Requires the re-implementation of a transition class โ— And the re-implementation of bool eventTest(QEvent *event) โ— Return true if the condition is met
  • 16. Integrated Computer Solutions Inc. www.ics.com Advantages of Using the State Machine Framework 16 โ— Allows for Graphical State Machine Designer Tooling. Examples: โ— Qt SCXML https://doc.qt.io/qt-5/qtscxml-overview.html, โ— KDAB State machine Editor https://github.com/KDAB/KDStateMachineEditor โ— Would allow for analysis of the overall logic โ— Allows for easy and precise documentation of requirements
  • 17. Integrated Computer Solutions Inc. www.ics.com Thank you! 17 Any questions?