1
<?xml version="1.0" encoding="iso-8859-1"?>
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 -->
7
<title>Qt 4.6: Event Transitions Example</title>
8
<link href="classic.css" rel="stylesheet" type="text/css" />
11
<table border="0" cellpadding="0" cellspacing="0" width="100%">
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"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="functions.html"><font color="#004faf">All Functions</font></a> · <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>
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>
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
26
Window(QWidget *parent = 0)
29
QPushButton *button = new QPushButton(this);
30
button->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);
34
QState *s1 = new QState();
35
s1->assignProperty(button, "text", "Outside");
37
QState *s2 = new QState();
38
s2->assignProperty(button, "text", "Inside");</pre>
39
<p>Two states, <tt>s1</tt> and <tt>s2</tt>, are created; upon entry they will assign "Outside" and "Inside" to the button's text, respectively.</p>
40
<pre> QEventTransition *enterTransition = new QEventTransition(button, QEvent::Enter);
41
enterTransition->setTargetState(s2);
42
s1->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->setTargetState(s1);
46
s2->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->assignProperty(button, "text", "Pressing...");
51
QEventTransition *pressTransition = new QEventTransition(button, QEvent::MouseButtonPress);
52
pressTransition->setTargetState(s3);
53
s2->addTransition(pressTransition);
55
QEventTransition *releaseTransition = new QEventTransition(button, QEvent::MouseButtonRelease);
56
releaseTransition->setTargetState(s2);
57
s3->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->addState(s1);
60
machine->addState(s2);
61
machine->addState(s3);
63
machine->setInitialState(s1);
67
<p>Finally, the states are added to the machine as top-level states, the initial state is set to be <tt>s1</tt> ("Outside"), and the machine is started.</p>
68
<pre> int main(int argc, char **argv)
70
QApplication app(argc, argv);
72
window.resize(300, 300);
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 © 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>