File: statemachine-eventtransitions.html

package info (click to toggle)
qt4-x11 4%3A4.8.2%2Bdfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 701,696 kB
  • sloc: cpp: 2,686,179; ansic: 375,485; python: 25,859; sh: 19,349; xml: 17,091; perl: 14,765; yacc: 5,383; asm: 5,038; makefile: 1,259; lex: 555; ruby: 526; objc: 347; cs: 112; pascal: 112; php: 54; sed: 34
file content (125 lines) | stat: -rw-r--r-- 11,429 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- eventtransitions.qdoc -->
  <title>Qt 4.8: Event Transitions Example</title>
  <link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="content"> 
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
  </div>
  <div class="breadcrumb toolblock">
    <ul>
      <li class="first"><a href="index.html">Home</a></li>
      <!--  Breadcrumbs go here -->
<li><a href="all-examples.html">Examples</a></li>
<li>Event Transitions Example</li>
    </ul>
  </div>
</div>
<div class="content mainContent">
<h1 class="title">Event Transitions Example</h1>
<span class="subtitle"></span>
<!-- $$$statemachine/eventtransitions-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="statemachine-eventtransitions-main-cpp.html">statemachine/eventtransitions/main.cpp</a></li>
<li><a href="statemachine-eventtransitions-eventtransitions-pro.html">statemachine/eventtransitions/eventtransitions.pro</a></li>
</ul>
<p>The Event Transitions example shows how to use event transitions, a feature of <a href="statemachine-api.html">The State Machine Framework</a>.<pre class="cpp"> <span class="keyword">class</span> Window : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span>
 {
 <span class="keyword">public</span>:
     Window(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>)
         : <span class="type"><a href="qwidget.html">QWidget</a></span>(parent)
     {
         <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>button <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(<span class="keyword">this</span>);
         button<span class="operator">-</span><span class="operator">&gt;</span>setSizePolicy(<span class="type"><a href="qsizepolicy.html">QSizePolicy</a></span><span class="operator">::</span>Expanding<span class="operator">,</span> <span class="type"><a href="qsizepolicy.html">QSizePolicy</a></span><span class="operator">::</span>Expanding);

         <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span> <span class="operator">*</span>layout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span>;
         layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(button);
         layout<span class="operator">-</span><span class="operator">&gt;</span>setContentsMargins(<span class="number">80</span><span class="operator">,</span> <span class="number">80</span><span class="operator">,</span> <span class="number">80</span><span class="operator">,</span> <span class="number">80</span>);
         setLayout(layout);</pre>
<p>The <tt>Window</tt> class's constructors begins by creating a button.</p>
<pre class="cpp">         <span class="type"><a href="qstatemachine.html">QStateMachine</a></span> <span class="operator">*</span>machine <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstatemachine.html">QStateMachine</a></span>(<span class="keyword">this</span>);

         <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s1 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();
         s1<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(button<span class="operator">,</span> <span class="string">&quot;text&quot;</span><span class="operator">,</span> <span class="string">&quot;Outside&quot;</span>);

         <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s2 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();
         s2<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(button<span class="operator">,</span> <span class="string">&quot;text&quot;</span><span class="operator">,</span> <span class="string">&quot;Inside&quot;</span>);</pre>
<p>Two states, <tt>s1</tt> and <tt>s2</tt>, are created; upon entry they will assign &quot;Outside&quot; and &quot;Inside&quot; to the button's text, respectively.</p>
<pre class="cpp">         <span class="type"><a href="qeventtransition.html">QEventTransition</a></span> <span class="operator">*</span>enterTransition <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qeventtransition.html">QEventTransition</a></span>(button<span class="operator">,</span> <span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>Enter);
         enterTransition<span class="operator">-</span><span class="operator">&gt;</span>setTargetState(s2);
         s1<span class="operator">-</span><span class="operator">&gt;</span>addTransition(enterTransition);</pre>
<p>When the button receives an event of type <a href="qevent.html#Type-enum">QEvent::Enter</a> and the state machine is in state <tt>s1</tt>, the machine will transition to state <tt>s2</tt>.</p>
<pre class="cpp">         <span class="type"><a href="qeventtransition.html">QEventTransition</a></span> <span class="operator">*</span>leaveTransition <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qeventtransition.html">QEventTransition</a></span>(button<span class="operator">,</span> <span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>Leave);
         leaveTransition<span class="operator">-</span><span class="operator">&gt;</span>setTargetState(s1);
         s2<span class="operator">-</span><span class="operator">&gt;</span>addTransition(leaveTransition);</pre>
<p>When the button receives an event of type <a href="qevent.html#Type-enum">QEvent::Leave</a> and the state machine is in state <tt>s2</tt>, the machine will transition back to state <tt>s1</tt>.</p>
<pre class="cpp">         <span class="type"><a href="qstate.html">QState</a></span> <span class="operator">*</span>s3 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qstate.html">QState</a></span>();
         s3<span class="operator">-</span><span class="operator">&gt;</span>assignProperty(button<span class="operator">,</span> <span class="string">&quot;text&quot;</span><span class="operator">,</span> <span class="string">&quot;Pressing...&quot;</span>);

         <span class="type"><a href="qeventtransition.html">QEventTransition</a></span> <span class="operator">*</span>pressTransition <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qeventtransition.html">QEventTransition</a></span>(button<span class="operator">,</span> <span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>MouseButtonPress);
         pressTransition<span class="operator">-</span><span class="operator">&gt;</span>setTargetState(s3);
         s2<span class="operator">-</span><span class="operator">&gt;</span>addTransition(pressTransition);

         <span class="type"><a href="qeventtransition.html">QEventTransition</a></span> <span class="operator">*</span>releaseTransition <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qeventtransition.html">QEventTransition</a></span>(button<span class="operator">,</span> <span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>MouseButtonRelease);
         releaseTransition<span class="operator">-</span><span class="operator">&gt;</span>setTargetState(s2);
         s3<span class="operator">-</span><span class="operator">&gt;</span>addTransition(releaseTransition);</pre>
<p>Next, the state <tt>s3</tt> is created. <tt>s3</tt> will be entered when the button receives an event of type <a href="qevent.html#Type-enum">QEvent::MouseButtonPress</a> and the state machine is in state <tt>s2</tt>. When the button receives an event of type <a href="qevent.html#Type-enum">QEvent::MouseButtonRelease</a> and the state machine is in state <tt>s3</tt>, the machine will transition back to state <tt>s2</tt>.</p>
<pre class="cpp">         machine<span class="operator">-</span><span class="operator">&gt;</span>addState(s1);
         machine<span class="operator">-</span><span class="operator">&gt;</span>addState(s2);
         machine<span class="operator">-</span><span class="operator">&gt;</span>addState(s3);

         machine<span class="operator">-</span><span class="operator">&gt;</span>setInitialState(s1);
         machine<span class="operator">-</span><span class="operator">&gt;</span>start();
     }
 };</pre>
<p>Finally, the states are added to the machine as top-level states, the initial state is set to be <tt>s1</tt> (&quot;Outside&quot;), and the machine is started.</p>
<pre class="cpp"> <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span><span class="operator">*</span>argv)
 {
     <span class="type"><a href="qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv);
     Window window;
     window<span class="operator">.</span>resize(<span class="number">300</span><span class="operator">,</span> <span class="number">300</span>);
 <span class="preprocessor">#if defined(Q_OS_SYMBIAN)</span>
     window<span class="operator">.</span>showMaximized();
 <span class="preprocessor">#else</span>
     window<span class="operator">.</span>show();
 <span class="preprocessor">#endif</span>

     <span class="keyword">return</span> app<span class="operator">.</span>exec();
 }</pre>
<p>The main() function constructs a Window object and shows it.</p>
</div>
<!-- @@@statemachine/eventtransitions -->
  <div class="ft">
    <span></span>
  </div>
</div> 
<div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2012 Nokia Corporation and/or its
      subsidiaries. Documentation contributions included herein are the copyrights of
      their respective owners.</p>
    <br />
    <p>
      The documentation provided herein is licensed under the terms of the
      <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
      License version 1.3</a> as published by the Free Software Foundation.</p>
    <p>
      Documentation sources may be obtained from <a href="http://www.qt-project.org">
      www.qt-project.org</a>.</p>
    <br />
    <p>
      Nokia, Qt and their respective logos are trademarks of Nokia Corporation 
      in Finland and/or other countries worldwide. All other trademarks are property
      of their respective owners. <a title="Privacy Policy"
      href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
</body>
</html>