1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the documentation of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:FDL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Free Documentation License Usage
18
** Alternatively, this file may be used under the terms of the GNU Free
19
** Documentation License version 1.3 as published by the Free Software
20
** Foundation and appearing in the file included in the packaging of
21
** this file. Please review the following information to ensure
22
** the GNU Free Documentation License version 1.3 requirements
23
** will be met: http://www.gnu.org/copyleft/fdl.html.
26
****************************************************************************/
29
\example statemachine/factorial
30
\title Factorial States Example
32
\brief The Factorial States example shows how to use \l{The State Machine
33
Framework} to calculate the factorial of an integer.
35
The statechart for calculating the factorial looks as follows:
37
\image factorial-example.png
39
\caption This is a caption
42
In other words, the state machine calculates the factorial of 6 and prints
45
\snippet statemachine/factorial/main.cpp 0
47
The Factorial class is used to hold the data of the computation, \c x and
48
\c fac. It also provides a signal that's emitted whenever the value of \c
51
\snippet statemachine/factorial/main.cpp 1
53
The FactorialLoopTransition class implements the guard (\c x > 1) and
54
calculations (\c fac = \c x * \c fac; \c x = \c x - 1) of the factorial
57
\snippet statemachine/factorial/main.cpp 2
59
The FactorialDoneTransition class implements the guard (\c x <= 1) that
60
terminates the factorial computation. It also prints the final result to
63
\snippet statemachine/factorial/main.cpp 3
65
The application's main() function first creates the application object, a
66
Factorial object and a state machine.
68
\snippet statemachine/factorial/main.cpp 4
70
The \c compute state is created, and the initial values of \c x and \c fac
71
are defined. A FactorialLoopTransition object is created and added to the
74
\snippet statemachine/factorial/main.cpp 5
76
A final state, \c done, is created, and a FactorialDoneTransition object
77
is created with \c done as its target state. The transition is then added
78
to the \c compute state.
80
\snippet statemachine/factorial/main.cpp 6
82
The machine's initial state is set to be the \c compute state. We connect
83
the QStateMachine::finished() signal to the QCoreApplication::quit() slot,
84
so the application will quit when the state machine's work is
85
done. Finally, the state machine is started, and the application's event