State.active is false in State.onEntered()



  • Hi,

    when entering a state the state is not active in its onEntered function.

    import QtQuick 2.5
    import QtQuick.Window 2.2
    import QtQml.StateMachine 1.0
    
    Window {
        visible: true
    
        MainForm {
            id: mainForm
            anchors.fill: parent
    
            StateMachine {
    
                running: true
                initialState: state1
    
                State {
                    id: state1
                    SignalTransition {
                        signal: mainForm.mouseArea.clicked
                        targetState: state2
                    }
                }
    
                State {
                    id: state2
                    onEntered: {
                        console.log("state2.active: " + state2.active)
                    }
                }
            }
        }
    }
    

    Because in QAbstractStatePrivate::emitEntered() the entered() signal ist emited before activeChanged() is emited.

    void QAbstractStatePrivate::emitEntered()
    {
        Q_Q(QAbstractState);
        emit q->entered(QAbstractState::QPrivateSignal());
        if (!active) {
            active = true;
            emit q->activeChanged(true);
        }
    }
    

    Is there any reason?



  • Think of 'entering' a state as crossing the boundary between the outside into the box that corresponds to the new state. Or even more concrete, think of a state 'InsideTheHouse', and entering that state as going through some door. You need to enter before you are actually in the new state, just as you have to pass the door, in order to get to the actual state of 'InsideTheHouse'.

    This is a quite general pattern used by any state machine implementation I ever came across. I'm not entirely sure though whether the default texts about software patterns explain this very clearly though, as it really is more of an implementation detail than a generic pattern.


Log in to reply
 

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