Ping Pong States Example

This example implements a statechart where two states communicate by posting events to the state machine. The state chart looks as follows:

Image non disponible

The pinger and ponger states are parallel states, i.e. they are entered simultaneously and will take transitions independently of eachother.

The pinger state will post the first ping event upon entry; the ponger state will respond by posting a pong event; this will cause the pinger state to post a new ping event; and so on.

 
Sélectionnez
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 and PongEvent.

 
Sélectionnez
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 a PingEvent to the state machine when the state is entered.

 
Sélectionnez
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 type PingEvent, and that posts a PongEvent (with a delay of 500 milliseconds) to the state machine when the transition is triggered.

 
Sélectionnez
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 type PongEvent, and that posts a PingEvent (with a delay of 500 milliseconds) to the state machine when the transition is triggered.

 
Sélectionnez
int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);

    QStateMachine machine;
    QState *group = new QState(QState::Par