source: trunk/doc/src/frameworks-technologies/statemachine.qdoc@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

  • Property svn:eol-style set to native
File size: 25.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43 \group statemachine
44 \title State Machine Classes
45*/
46
47/*!
48 \page statemachine-api.html
49 \title The State Machine Framework
50 \brief An overview of the State Machine framework for constructing and executing state graphs.
51
52 \ingroup frameworks-technologies
53
54 \tableofcontents
55
56 The State Machine framework provides classes for creating and executing
57 state graphs. The concepts and notation are based on those from Harel's
58 \l{Statecharts: A visual formalism for complex systems}{Statecharts}, which
59 is also the basis of UML state diagrams. The semantics of state machine
60 execution are based on \l{State Chart XML: State Machine Notation for
61 Control Abstraction}{State Chart XML (SCXML)}.
62
63 Statecharts provide a graphical way of modeling how a system reacts to
64 stimuli. This is done by defining the possible \e states that the system can
65 be in, and how the system can move from one state to another (\e transitions
66 between states). A key characteristic of event-driven systems (such as Qt
67 applications) is that behavior often depends not only on the last or current
68 event, but also the events that preceded it. With statecharts, this
69 information is easy to express.
70
71 The State Machine framework provides an API and execution model that can be
72 used to effectively embed the elements and semantics of statecharts in Qt
73 applications. The framework integrates tightly with Qt's meta-object system;
74 for example, transitions between states can be triggered by signals, and
75 states can be configured to set properties and invoke methods on QObjects.
76 Qt's event system is used to drive the state machines.
77
78 The state graph in the State Machine framework is hierarchical. States can be nested inside of
79 other states, and the current configuration of the state machine consists of the set of states
80 which are currently active. All the states in a valid configuration of the state machine will
81 have a common ancestor.
82
83 \section1 Classes in the State Machine Framework
84
85 These classes are provided by qt for creating event-driven state machines.
86
87 \annotatedlist statemachine
88
89 \section1 A Simple State Machine
90
91 To demonstrate the core functionality of the State Machine API, let's look
92 at a small example: A state machine with three states, \c s1, \c s2 and \c
93 s3. The state machine is controlled by a single QPushButton; when the button
94 is clicked, the machine transitions to another state. Initially, the state
95 machine is in state \c s1. The statechart for this machine is as follows:
96
97 \img statemachine-button.png
98 \omit
99 \caption This is a caption
100 \endomit
101
102 The following snippet shows the code needed to create such a state machine.
103 First, we create the state machine and states:
104
105 \snippet doc/src/snippets/statemachine/main.cpp 0
106
107 Then, we create the transitions by using the QState::addTransition()
108 function:
109
110 \snippet doc/src/snippets/statemachine/main.cpp 1
111
112 Next, we add the states to the machine and set the machine's initial state:
113
114 \snippet doc/src/snippets/statemachine/main.cpp 2
115
116 Finally, we start the state machine:
117
118 \snippet doc/src/snippets/statemachine/main.cpp 3
119
120 The state machine executes asynchronously, i.e. it becomes part of your
121 application's event loop.
122
123 \section1 Doing Useful Work on State Entry and Exit
124
125 The above state machine merely transitions from one state to another, it
126 doesn't perform any operations. The QState::assignProperty() function can be
127 used to have a state set a property of a QObject when the state is
128 entered. In the following snippet, the value that should be assigned to a
129 QLabel's text property is specified for each state:
130
131 \snippet doc/src/snippets/statemachine/main.cpp 4
132
133 When any of the states is entered, the label's text will be changed
134 accordingly.
135
136 The QState::entered() signal is emitted when the state is entered, and the
137 QState::exited() signal is emitted when the state is exited. In the
138 following snippet, the button's showMaximized() slot will be called when
139 state \c s3 is entered, and the button's showMinimized() slot will be called
140 when \c s3 is exited:
141
142 \snippet doc/src/snippets/statemachine/main.cpp 5
143
144 Custom states can reimplement QAbstractState::onEntry() and
145 QAbstractState::onExit().
146
147 \section1 State Machines That Finish
148
149 The state machine defined in the previous section never finishes. In order
150 for a state machine to be able to finish, it needs to have a top-level \e
151 final state (QFinalState object). When the state machine enters a top-level
152 final state, the machine will emit the QStateMachine::finished() signal and
153 halt.
154