Ping Pong States Example¶
The Ping Pong States example shows how to use parallel states together with custom events and transitions in The State Machine Framework .
This example implements a statechart where two states communicate by posting events to the state machine. The state chart looks as follows:
The
pinger
andponger
states are parallel states, i.e. they are entered simultaneously and will take transitions independently of eachother.The
pinger
state will post the firstping
event upon entry; theponger
state will respond by posting apong
event; this will cause thepinger
state to post a newping
event; and so on.class PingEvent : public QEvent { public: PingEvent() : QEvent(QEvent::Type(QEvent::User+2)) {} }; class PongEvent : public QEvent { public: PongEvent() : QEvent(QEvent::Type(QEvent::User+3)) {} };Two custom events are defined,
PingEvent
andPongEvent
.class Pinger : public QState { public: Pinger(QState *parent) : QState(parent) {} protected: void onEntry(QEvent *) override { machine()->postEvent(new PingEvent()); fprintf(stdout, "ping?\n"); } };The
Pinger
class defines a state that posts aPingEvent
to the state machine when the state is entered.class PingTransition : public QAbstractTransition { public: PingTransition() {} protected: bool eventTest(QEvent *e) override { return (e->type() == QEvent::User+2); } void onTransition(QEvent *) override { machine()->postDelayedEvent(new PongEvent(), 500); fprintf(stdout, "pong!\n"); } };The
PingTransition
class defines a transition that is triggered by events of typePingEvent
, and that posts aPongEvent
(with a delay of 500 milliseconds) to the state machine when the transition is triggered.class PongTransition : public QAbstractTransition { public: PongTransition() {} protected: bool eventTest(QEvent *e) override { return (e->type() == QEvent::User+3); } void onTransition(QEvent *) override { machine()->postDelayedEvent(new PingEvent(), 500); fprintf(stdout, "ping?\n"); } };The
PongTransition
class defines a transition that is triggered by events of typePongEvent
, and that posts aPingEvent
(with a delay of 500 milliseconds) to the state machine when the transition is triggered.int main(int argc, char **argv) { QCoreApplication app(argc, argv); QStateMachine machine; QState *group = new QState(QState::ParallelStates); group->setObjectName("group");The main() function begins by creating a state machine and a parallel state group.
Pinger *pinger = new Pinger(group); pinger->setObjectName("pinger"); pinger->addTransition(new PongTransition()); QState *ponger = new QState(group); ponger->setObjectName("ponger"); ponger->addTransition(new PingTransition());Next, the
pinger
andponger
states are created, with the parallel state group as their parent state. Note that the transitions are targetless . When such a transition is triggered, the source state won’t be exited and re-entered; only the transition’s onTransition() function will be called, and the state machine’s configuration will remain the same, which is precisely what we want in this case.machine.addState(group); machine.setInitialState(group); machine.start(); return app.exec(); }Finally, the group is added to the state machine, the machine is started, and the application event loop is entered.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.