0

I have state machine with three states s1, s2, s3 and s4. From states s1, s2 and s3 machine can enter error state.

QStateMachine machine;
QState *s1 = new QState();
QState *s2 = new QState();
QState *s3 = new QState();
QFinalState *s4 = new QFinalState();
s1->addTransition(object, SIGNAL(done()), s2);
s2->addTransition(object, SIGNAL(done()), s3);
s3->addTransition(object, SIGNAL(done()), s4);
machine.addState(s1);
machine.addState(s2);
machine.addState(s3);
machine.addState(s4);
machine.setInitialState(s1);

What is the best way to do this? I was googling for that, but no luck. is it ok to do something like this?

QFinalState *sx = new QFinalState();
machine.addState(sx);
s1->addTransition(object, SIGNAL(error()), sx );
s2->addTransition(object, SIGNAL(error()), sx );
s3->addTransition(object, SIGNAL(error()), sx );
Sir Digby Chicken Caesar
  • 3,063
  • 1
  • 23
  • 31
zvjerka24
  • 1,772
  • 1
  • 21
  • 27
  • Sometimes it may be useful to put state1,state2,state3 into one super state. then you just add one transition from the super state to the error state. all sub states will "follow" this transition too. – Del Pedro Apr 05 '13 at 14:20

1 Answers1

1
QFinalState *sx = new QFinalState();
machine.addState(sx);
s1->addTransition(object, SIGNAL(error()), sx );
s2->addTransition(object, SIGNAL(error()), sx );
s3->addTransition(object, SIGNAL(error()), sx );

That looks like it will work, yes. Having to call addTransition() for all your states might be a bit cumbersome, so you could group all your states by adding a parent state to them. Then you would only need one error transition on the parent.

Thomas McGuire
  • 5,308
  • 26
  • 45