0

From Qt state machine documentation, I can set, in a main state, the initial substate.

QStateMachine machine;
QState *s1 = new QState();
machine.addState(s1);
machine.setInitialState(s1);

I can also add guard to transition between different states.

But how can I add a guard on an initial state.

QState *s1 = new QState();
QState *s2 = new QState();
machine.addState(s1);
machine.addState(s2);
machine.setInitialState(s1, ifblabla_istrue);
machine.setInitialState(s2, ifblabla_isfalse);
Misch
  • 10,350
  • 4
  • 35
  • 49
ruddy
  • 135
  • 4
  • 12
  • Why do you want to do this ? Isn't your initial state fixed ? – p.i.g. May 13 '15 at 21:16
  • My application is a phone basically. User can press prior to a call the AutoAnswer button. If a call comes in and it goes to the mega INCALLSESSION state. That INCALLSESSION initial sub state should be TALKING if AutoAnswer=true and RINGING if AutoAnswer=false – ruddy May 13 '15 at 21:20
  • Okay, but you need to know where to start your machine. So I think you need to setup your state machine and when you want to start it, you need to setup you initial state there with a conditional expression. – p.i.g. May 13 '15 at 21:23
  • My state machine is well setup. What I'm talking about here is substates inside another state. So, I have states "IDLE" and "InCallSession". Then "Talking" and "Ringing" are substates of "InCallSession". So, at the end, then the EventNewCall is triggered, the state machine should go to "Ringing" or "Talking" based on the AutoAnswer value. – ruddy May 13 '15 at 21:27
  • You can always inherit from QStateMachine class and overwrite this method like: { static bool initialized = false; if (false == initialized){ initialized = true; QStateMachine::setInitialState(..); } } – fider Sep 17 '15 at 07:33

1 Answers1

0

I've worked with statecharts frameworks and it doesn't seem like Qt treats this as normal transition. I think you'll have to create a "init" puedo-state that setInitialState will go into and then you have to set guards when you transition out of that state.

Francis
  • 21
  • 5