2009-09-11

State Machine in Qt

Qt provides a two classes to implements state machines.
QtStateMachine offers the basic APIs for state machine mechanism, while QtScriptedStateMachine is a subclass of QtStateMachine and adds the missing parts to support SCXML. Max tried to implement the following state machine. The code is not yet tested, ;) Hope it works!


//selection state groups
QtState *pStateSelection = new QtState();
QtState *pStateSearch = new QtState(pStateSelection);
QtState *pStateBrowse = new QtState(pStateSelection);
pStateSelection->setInitialState(pStateBrowse);

//processing state groups
QtState *pStateProcessing = new QtState();
QtState *pStateMaterial = new QtState(pStateProcessing);
QtState *pStateProcedure = new QtState(pStateProcessing);
pStateProcessing->setInitialState(pStateMaterial);

//transitions from "Selection" state
pStateSelection->addTransition(buttonStart, SIGNAL(clicked()), pStateProcessing);

//transitions from "Browse" state
pStateBrowse->addTransition(buttonSearch, SIGNAL(clicked()), pStateSearch);

//transitions from "Search" state
pStateSearch->addTransition(buttonOK, SIGNAL(clicked()), pStateBrowse);
pStateSearch->addTransition(buttonCancel, SIGNAL(clicked()), pStateBrowse);

//transitions from "Processing" state
pStateProcessing->addTransition(buttonFinish, SIGNAL(clicked()), pStateSelection);
pStateProcessing->addTransition(buttonCancel, SIGNAL(clicked()), pStateSelection);

//transitions from "Material" state
pStateMaterial->addTransition(buttonProcess, SIGNAL(clicked()), pStateProcedure);

//transitions from "Procedure" state
pStateProcedure->addTransition(buttonMaterial, SIGNAL(clicked()), pStateMaterial);

//start the state-machine
QtStateMachine oMachine;
oMachine.addState(pStateSelection);
oMachine.addState(pStateProcessing);
aMachine.setInitialState(pStateSelection);

oMachine.start();

Reference:

No comments:

Post a Comment