~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/statemachine-eventtransitions.html

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- eventtransitions.qdoc -->
 
6
<head>
 
7
  <title>Qt 4.6: Event Transitions Example</title>
 
8
  <link href="classic.css" rel="stylesheet" type="text/css" />
 
9
</head>
 
10
<body>
 
11
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
12
<tr>
 
13
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
 
14
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <a href="overviews.html"><font color="#004faf">Overviews</font></a></td><td class="searchBar" align="right" valign="center"><form action="http://www.google.com/cse" id="cse-search-box"><div><input type="hidden" name="cx" value="000136343326384750312:dhbxnqlakyu" /><input type="hidden" name="ie" value="UTF-8" /><input type="text" name="q" size="31" /><input type="submit" name="sa" value="Search" /></div></form></td>
 
15
</tr></table><h1 class="title">Event Transitions Example<br /><span class="subtitle"></span>
 
16
</h1>
 
17
<p>Files:</p>
 
18
<ul>
 
19
<li><a href="statemachine-eventtransitions-main-cpp.html">statemachine/eventtransitions/main.cpp</a></li>
 
20
<li><a href="statemachine-eventtransitions-eventtransitions-pro.html">statemachine/eventtransitions/eventtransitions.pro</a></li>
 
21
</ul>
 
22
<p>The Event Transitions example shows how to use event transitions, a feature of <a href="statemachine-api.html">The State Machine Framework</a>.</p>
 
23
<pre> class Window : public QWidget
 
24
 {
 
25
 public:
 
26
     Window(QWidget *parent = 0)
 
27
         : QWidget(parent)
 
28
     {
 
29
         QPushButton *button = new QPushButton(this);
 
30
         button-&gt;setGeometry(QRect(100, 100, 100, 100));</pre>
 
31
<p>The <tt>Window</tt> class's constructors begins by creating a button.</p>
 
32
<pre>         QStateMachine *machine = new QStateMachine(this);
 
33
 
 
34
         QState *s1 = new QState();
 
35
         s1-&gt;assignProperty(button, &quot;text&quot;, &quot;Outside&quot;);
 
36
 
 
37
         QState *s2 = new QState();
 
38
         s2-&gt;assignProperty(button, &quot;text&quot;, &quot;Inside&quot;);</pre>
 
39
<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>
 
40
<pre>         QEventTransition *enterTransition = new QEventTransition(button, QEvent::Enter);
 
41
         enterTransition-&gt;setTargetState(s2);
 
42
         s1-&gt;addTransition(enterTransition);</pre>
 
43
<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>
 
44
<pre>         QEventTransition *leaveTransition = new QEventTransition(button, QEvent::Leave);
 
45
         leaveTransition-&gt;setTargetState(s1);
 
46
         s2-&gt;addTransition(leaveTransition);</pre>
 
47
<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>
 
48
<pre>         QState *s3 = new QState();
 
49
         s3-&gt;assignProperty(button, &quot;text&quot;, &quot;Pressing...&quot;);
 
50
 
 
51
         QEventTransition *pressTransition = new QEventTransition(button, QEvent::MouseButtonPress);
 
52
         pressTransition-&gt;setTargetState(s3);
 
53
         s2-&gt;addTransition(pressTransition);
 
54
 
 
55
         QEventTransition *releaseTransition = new QEventTransition(button, QEvent::MouseButtonRelease);
 
56
         releaseTransition-&gt;setTargetState(s2);
 
57
         s3-&gt;addTransition(releaseTransition);</pre>
 
58
<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>
 
59
<pre>         machine-&gt;addState(s1);
 
60
         machine-&gt;addState(s2);
 
61
         machine-&gt;addState(s3);
 
62
 
 
63
         machine-&gt;setInitialState(s1);
 
64
         machine-&gt;start();
 
65
     }
 
66
 };</pre>
 
67
<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>
 
68
<pre> int main(int argc, char **argv)
 
69
 {
 
70
     QApplication app(argc, argv);
 
71
     Window window;
 
72
     window.resize(300, 300);
 
73
     window.show();
 
74
 
 
75
     return app.exec();
 
76
 }</pre>
 
77
<p>The main() function constructs a Window object and shows it.</p>
 
78
<p /><address><hr /><div align="center">
 
79
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
80
<td width="40%" align="left">Copyright &copy; 2009 Nokia Corporation and/or its subsidiary(-ies)</td>
 
81
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
82
<td width="40%" align="right"><div align="right">Qt 4.6.0</div></td>
 
83
<script type="text/javascript" src="http://www.google.com/jsapi"></script><script type="text/javascript">google.load("elements", "1", {packages: "transliteration"});</script><script type="text/javascript" src="http://www.google.com/coop/cse/t13n?form=cse-search-box&t13n_langs=en"></script><script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en"></script></tr></table></div></address></body>
 
84
</html>