Changing state of QStateMachine based on a signal argument.



  • In my project I'm using QStateMachine to control GUI. It works fine, but lately I've run on one problem. I've got signal @finished(ushort status, QString msg);@ , which informs that worker had finished action (and if it succeed or failed). Transition is based on this signal, and it should choose different states based on success or failure. How should I achieve that? Is defining 2 signals (succes(), fail()) only solution?



  • No need to define two signals.

    What I would do is to make the two transitions guarded transitions. This means that for these transitions you should derive your own custom transition class from QSignalTransition and override the bool QAbstractTransition::eventTest( QEvent* e ) function.

    This function gets called by the state machine framework when checkign which transitions should occur (ie each time an event is delivered and the SM is in the source state of the transition). If the transition meets all of your criteria then return true, otherwise return false.

    In your simple case a single class with a settable boolean property that lets each object know if it should be checking for success or failure is all that is needed. In more complex cases you may need a special transition class for each possible transition. It depends on how easily your tests can be abstracted.

    In your case something like the following should do the trick:

    @
    class BooleanSignalTransition : public QSignalTransition
    {
    Q_OBJECT
    BooleanSignalTransition( QState* sourceState, const char* signal, QAbstractState* targetState )
    : QSignalTransition( sourceState, signal, targetState )
    m_test( true ) // Check for success by default
    {}

    void setTest( bool b ) { m_test = b; }
    bool test() const { return m_test; }
    
    virtual bool eventTest( QEvent* e )
    {
        // Let QSignalTransition check it is the correct signal first of all
        if ( !QSignalTransition::eventTest( e ) )
            return false;
    
        // Now check that the argument has the correct value
        QStateMachine::SignalEvent* se = static_cast<QStateMachine::SignalEvent*>( e );
        bool value = se->arguments().at( 0 ).value<bool>();
        return ( value == m_test );
    }
    

    private:
    bool m_test;
    };
    @

    Then when you construct the states in question make two instances of the above instead of your usual QSignalTransition objects and for one of them call setTest( false ).

    Hope that helps.



  • nice post. thanks



  • You're welcome. I have the above to be a very common pattern with state machines. Often though the transitions guard against receiving a specific event type rather than a signal. See this "doc note":http://developer.qt.nokia.com/doc/qt-4.7/statemachine-api.html#note-36 for some helper classes to do this easily.



  • Good answer indeed. Just wish I had found it sooner...

    To me it seems like a common problem when using state machines, adding the example to the documentation on the state machine framework wouldn't hurt.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.