本页内容

HistoryState QML Type

HistoryState 类型提供了一种返回先前处于活动状态的子状态的方法。更多内容...

Import Statement: import QtQml.StateMachine 6.12
Inherits:

QAbstractState

属性

详细说明

历史状态是一种伪状态,它表示父状态上次退出时所处的子状态。以历史状态为目标的转换,实际上是向父状态的其他子状态之一进行的转换。HistoryState 是Qt State Machine QML API 的一部分。

使用defaultState 属性来设置当父状态从未被进入时应进入的状态。

用法示例

import QtQuick
import QtQml.StateMachine as DSM

Rectangle {
    Button {
        anchors.fill: parent
        id: button
        text: "Press me"
        DSM.StateMachine {
            id: stateMachine
            initialState: parentState
            running: true
            DSM.State {
                id: parentState
                initialState: child2
                onEntered: console.log("parentState entered")
                onExited: console.log("parentState exited")
                DSM.State {
                    id: child1
                    onEntered: console.log("child1 entered")
                    onExited: console.log("child1 exited")
                }
                DSM.State {
                    id: child2
                    onEntered: console.log("child2 entered")
                    onExited: console.log("child2 exited")
                }
                DSM.HistoryState {
                    id: historyState
                    defaultState: child1
                }
                DSM.SignalTransition {
                    targetState: historyState

                    // Clicking the button will cause the state machine to enter the child state
                    // that parentState was in the last time parentState was exited, or the history state's default
                    // state if parentState has never been entered.
                    signal: button.clicked
                }
            }
        }
    }
}

默认情况下,历史状态是浅层状态,这意味着它不会记住嵌套状态。这可以通过historyType 属性进行配置。

另请参阅 StateMachine 和State 。

属性文档

defaultState : QAbstractState

此历史状态的默认状态。

默认状态指明,如果父状态此前从未被进入过,则应过渡到的状态。

historyType : enumeration

此历史记录状态所记录的历史记录类型。

该属性的默认值为HistoryState.ShallowHistory。

此枚举指定HistoryState 记录的历史记录类型。

  • HistoryState.ShallowHistory 仅记录父状态的直接子状态。在这种情况下,以历史状态为目标的转换将最终到达父状态上次退出时所在的直接子状态。这是默认设置。
  • HistoryState.DeepHistory 记录嵌套状态。在此情况下,以历史状态为目标的转换将最终进入父状态上次退出时所处的最深层嵌套后代状态。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.