File: script-qstetrix.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 (126 lines) | stat: -rw-r--r-- 10,596 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
126
<?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" />
<!-- qtscripttetrix.qdoc -->
  <title>Qt 4.8: Qt Script Tetrix 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>Qt Script Tetrix Example</li>
    </ul>
  </div>
</div>
<div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#overview">Overview</a></li>
<li class="level1"><a href="#setting-up-the-gui">Setting up the GUI</a></li>
</ul>
</div>
<h1 class="title">Qt Script Tetrix Example</h1>
<span class="subtitle"></span>
<!-- $$$script/qstetrix-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="script-qstetrix-tetrixboard-cpp.html">script/qstetrix/tetrixboard.cpp</a></li>
<li><a href="script-qstetrix-tetrixboard-h.html">script/qstetrix/tetrixboard.h</a></li>
<li><a href="script-qstetrix-tetrixboard-js.html">script/qstetrix/tetrixboard.js</a></li>
<li><a href="script-qstetrix-tetrixpiece-js.html">script/qstetrix/tetrixpiece.js</a></li>
<li><a href="script-qstetrix-tetrixwindow-js.html">script/qstetrix/tetrixwindow.js</a></li>
<li><a href="script-qstetrix-tetrixwindow-ui.html">script/qstetrix/tetrixwindow.ui</a></li>
<li><a href="script-qstetrix-main-cpp.html">script/qstetrix/main.cpp</a></li>
<li><a href="script-qstetrix-qstetrix-pro.html">script/qstetrix/qstetrix.pro</a></li>
<li><a href="script-qstetrix-tetrix-qrc.html">script/qstetrix/tetrix.qrc</a></li>
</ul>
<p>The QSTetrix example is a Qt Script version of the classic Tetrix game.<p class="centerAlign"><img src="images/tetrix-example.png" alt="" /></p><a name="overview"></a>
<h2>Overview</h2>
<p>The program logic in this example is a fairly straight port of the logic in the C++ <a href="widgets-tetrix.html">Tetrix Example</a>. You may find it useful to compare the implementations of the <tt>TetrixBoard</tt>, <tt>TetrixPiece</tt> and <tt>TetrixWindow</tt> classes to see how Qt Script is used to implement methods, call Qt functions, and emit signals.</p>
<a name="setting-up-the-gui"></a>
<h2>Setting up the GUI</h2>
<p>The graphical user interface is defined in a UI file, created using Qt Designer, and is set up in the example's C++ <tt>main.cpp</tt> file.</p>
<pre class="cpp"> <span class="keyword">class</span> TetrixUiLoader : <span class="keyword">public</span> <span class="type"><a href="quiloader.html">QUiLoader</a></span>
 {
 <span class="keyword">public</span>:
     TetrixUiLoader(<span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>)
         : <span class="type"><a href="quiloader.html">QUiLoader</a></span>(parent)
         { }
     <span class="keyword">virtual</span> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>createWidget(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>className<span class="operator">,</span> <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="operator">,</span>
                                   <span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>name <span class="operator">=</span> <span class="type"><a href="qstring.html">QString</a></span>())
     {
         <span class="keyword">if</span> (className <span class="operator">=</span><span class="operator">=</span> QLatin1String(<span class="string">&quot;TetrixBoard&quot;</span>)) {
             <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>board <span class="operator">=</span> <span class="keyword">new</span> TetrixBoard(parent);
             board<span class="operator">-</span><span class="operator">&gt;</span>setObjectName(name);
             <span class="keyword">return</span> board;
         }
         <span class="keyword">return</span> <span class="type"><a href="quiloader.html">QUiLoader</a></span><span class="operator">::</span>createWidget(className<span class="operator">,</span> parent<span class="operator">,</span> name);
     }
 };</pre>
<p>We define a custom UI loader that handles our <tt>TetrixBoard</tt> widget; this is the main component of the UI (where the pieces are drawn).</p>
<pre class="cpp">     <span class="type"><a href="qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv);
     <span class="type"><a href="qscriptengine.html">QScriptEngine</a></span> engine;

     <span class="type"><a href="qscriptvalue.html">QScriptValue</a></span> <span class="type"><a href="qt.html">Qt</a></span> <span class="operator">=</span> engine<span class="operator">.</span>newQMetaObject(<span class="type">QtMetaObject</span><span class="operator">::</span>get());
     <span class="type"><a href="qt.html">Qt</a></span><span class="operator">.</span>setProperty(<span class="string">&quot;App&quot;</span><span class="operator">,</span> engine<span class="operator">.</span>newQObject(<span class="operator">&amp;</span>app));
     engine<span class="operator">.</span>globalObject()<span class="operator">.</span>setProperty(<span class="string">&quot;Qt&quot;</span><span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span>);</pre>
<p>We initialize the script engine to have the Qt namespace, so that e.g&#x2e;, <a href="qt.html#Key-enum">Qt.Key_Left</a> will be available to script code. We also make the application object available (for the <a href="qcoreapplication.html#quit">quit()</a> slot).</p>
<pre class="cpp">     evaluateFile(engine<span class="operator">,</span> <span class="string">&quot;:/tetrixpiece.js&quot;</span>);
     evaluateFile(engine<span class="operator">,</span> <span class="string">&quot;:/tetrixboard.js&quot;</span>);
     evaluateFile(engine<span class="operator">,</span> <span class="string">&quot;:/tetrixwindow.js&quot;</span>);</pre>
<p>Several scripts are evaluated as part of the engine setup process. The <tt>tetrixpiece.js</tt> file contains the definition of the <tt>TetrixPiece</tt> class, which is used to populate the play field. The <tt>tetrixboard.js</tt> file contains the definition of the <tt>TetrixBoard</tt> class, which contains the main game logic. Finally, <tt>tetrixwindow.js</tt> contains the definition of the <tt>TetrixWindow</tt> class, which wires up the top-level widget.</p>
<pre class="cpp">     TetrixUiLoader loader;
     <span class="type"><a href="qfile.html">QFile</a></span> uiFile(<span class="string">&quot;:/tetrixwindow.ui&quot;</span>);
     uiFile<span class="operator">.</span>open(<span class="type"><a href="qiodevice.html">QIODevice</a></span><span class="operator">::</span>ReadOnly);
     <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>ui <span class="operator">=</span> loader<span class="operator">.</span>load(<span class="operator">&amp;</span>uiFile);
     uiFile<span class="operator">.</span>close();

     <span class="type"><a href="qscriptvalue.html">QScriptValue</a></span> ctor <span class="operator">=</span> engine<span class="operator">.</span>evaluate(<span class="string">&quot;TetrixWindow&quot;</span>);
     <span class="type"><a href="qscriptvalue.html">QScriptValue</a></span> scriptUi <span class="operator">=</span> engine<span class="operator">.</span>newQObject(ui<span class="operator">,</span> <span class="type"><a href="qscriptengine.html">QScriptEngine</a></span><span class="operator">::</span>ScriptOwnership);
     <span class="type"><a href="qscriptvalue.html">QScriptValue</a></span> tetrix <span class="operator">=</span> ctor<span class="operator">.</span>construct(<span class="type">QScriptValueList</span>() <span class="operator">&lt;</span><span class="operator">&lt;</span> scriptUi);</pre>
<p>A form is created from the UI file. A new <tt>TetrixWindow</tt> script object is then constructed, passing the form as its argument.</p>
<pre class="cpp">     ui<span class="operator">-</span><span class="operator">&gt;</span>resize(<span class="number">550</span><span class="operator">,</span> <span class="number">370</span>);
     ui<span class="operator">-</span><span class="operator">&gt;</span>show();

     qsrand(<span class="type"><a href="qtime.html">QTime</a></span>(<span class="number">0</span><span class="operator">,</span><span class="number">0</span><span class="operator">,</span><span class="number">0</span>)<span class="operator">.</span>secsTo(<span class="type"><a href="qtime.html">QTime</a></span><span class="operator">::</span>currentTime()));
     <span class="keyword">return</span> app<span class="operator">.</span>exec();</pre>
<p>The form is shown, and the event loop is entered.</p>
</div>
<!-- @@@script/qstetrix -->
  <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>