Create a new QSignalTransition instance and set its target state to the state you want to go to from the onEntry method. Create a new signal for your class to be used by the QSignalTransition. Add this new transition to the state with the onEntry function. In the onEntry function, emit the signal.
// MyStateMachine.h
signals:
void goToState2();
private slots:
void onState1Entered();
void onState2Entered();
private:
void setupStates();
private:
QStateMachine * pStateMachine;
//...
// MyStateMachine.cpp
void MyStateMachine::setupStates()
{
// pStateMachine is theQStateMachine instance
QState * pState1 = new QState( pStateMachine );
connect( pState1, SIGNAL( entered() ), this, SLOT( onState1Entered() ) );
QState * pState2 - new QState( pStateMachine );
connect( pState2, SIGNAL( entered() ), this, SLOT( onState2Entered() ) );
QSignalTransition * pState1To2 = new QSignalTransition( this, SIGNAL( goToState2() );
pState1To2->setTargetState( pState2 );
pState1->addTransition( pState1To2 );
}
void MyStateMachine::onState1Entered()
{
emit goToState2();
}
// MyStateMachine.h
signals:
void goToState2();
private slots:
void onState1Entered();
void onState2Entered();
private:
void setupStates();
private:
QStateMachine * pStateMachine;
//...
// MyStateMachine.cpp
void MyStateMachine::setupStates()
{
// pStateMachine is theQStateMachine instance
QState * pState1 = new QState( pStateMachine );
connect( pState1, SIGNAL( entered() ), this, SLOT( onState1Entered() ) );
QState * pState2 - new QState( pStateMachine );
connect( pState2, SIGNAL( entered() ), this, SLOT( onState2Entered() ) );
QSignalTransition * pState1To2 = new QSignalTransition( this, SIGNAL( goToState2() );
pState1To2->setTargetState( pState2 );
pState1->addTransition( pState1To2 );
}
void MyStateMachine::onState1Entered()
{
emit goToState2();
}
To copy to clipboard, switch view to plain text mode
As you see, you do not have to create a custom QState and override the onEntry() method. Everything you want to do can almost always be done using standard QState and QStateMachine from the Qt libraries. The code I posted above is adapted from a complex state machine I implemented to handle mouse and keyboard events in a graphics plot. Even though I called the class "MyStateMachine" in this example, it is not derived from QStateMachine. It instantiates a QStateMachine instance ("pStateMachine ") and adds all of the states and transitions to that.
Bookmarks