Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[QML] History is not recorded by HistoryState



  • Hi,
    I have problem with HistoryState.
    For simple code:

    import QtQuick 2.11
    import QtQuick.Window 2.11
    import QtQuick.Controls 1.4
    import QtQml.StateMachine 1.11 as DSM
    
    Window {
        visible: true
        width: 120
        height: 120
        title: qsTr("Hello World")
    
        Button
        {
            id: button1
            anchors.left: parent.left
            width: parent.width / 2
            height: parent.height
            text: "button1"
        }
    
        Button
        {
            id: button2
            anchors.right: parent.right
            width: parent.width / 2
            height: parent.height
            text: "button2"
        }
    
        DSM.StateMachine
        {
            id: stateMachine
            initialState: beginState
            running: true
    
            DSM.HistoryState
            {
                id: historyState
                defaultState: state1
            }
    
            DSM.State
            {
                id: beginState
                onEntered: console.log("beginState")
    
                DSM.SignalTransition
                {
                    targetState: state1
                    signal: button1.clicked
                }
    
                DSM.SignalTransition
                {
                    targetState: state2
                    signal: button2.clicked
                }
            }
    
            DSM.State
            {
                id: state1
                onEntered: console.log("state1")
    
                DSM.SignalTransition
                {
                    targetState: state2
                    signal: button1.clicked
                }
            }
    
            DSM.State
            {
                id: state2
                onEntered: console.log("state2")
    
                DSM.SignalTransition
                {
                    targetState: historyState
                    signal: button1.clicked
                }
    
                DSM.SignalTransition
                {
                    targetState: beginState
                    signal: button2.clicked
                }
            }
    
            DSM.FinalState {
                id: finalState
            }
            onFinished: Qt.quit()
        }
    }
    
    

    A transition with a history state is a transition to default state (state1). History is not recorded, why?
    What's wrong with my code?

    Qt version: 5.11.1



  • @Montjet
    I think you are not using the history state correctly.

    A history state should be defined inside a parent state, along with other children states
    as it is used to restore the child state of its parent state.

    You declare your history state outside any parent state...

    Have a look at this example:
    http://doc.qt.io/qt-5/qmlstatemachine.html#using-history-states



  • Thank you, it works.


Log in to reply