Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8e6051afcdb111a0317a58fb64c2abf5 > files > 5794

qt4-doc-4.6.3-0.2mdv2010.2.i586.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- qtscripttetrix.qdoc -->
<head>
  <title>Qt 4.6: Qt Script Tetrix Example</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<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>
<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></tr></table><h1 class="title">Qt Script Tetrix Example<br /><span class="subtitle"></span>
</h1>
<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>
<p align="center"><img src="images/tetrix-example.png" /></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 TetrixUiLoader : public QUiLoader
 {
 public:
     TetrixUiLoader(QObject *parent = 0)
         : QUiLoader(parent)
         { }
     virtual QWidget *createWidget(const QString &amp;className, QWidget *parent = 0,
                                   const QString &amp;name = QString())
     {
         if (className == QLatin1String(&quot;TetrixBoard&quot;)) {
             QWidget *board = new TetrixBoard(parent);
             board-&gt;setObjectName(name);
             return board;
         }
         return QUiLoader::createWidget(className, parent, 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>     QApplication app(argc, argv);
     QScriptEngine engine;

     QScriptValue Qt = engine.newQMetaObject(QtMetaObject::get());
     Qt.setProperty(&quot;App&quot;, engine.newQObject(&amp;app));
     engine.globalObject().setProperty(&quot;Qt&quot;, Qt);</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>     evaluateFile(engine, &quot;:/tetrixpiece.js&quot;);
     evaluateFile(engine, &quot;:/tetrixboard.js&quot;);
     evaluateFile(engine, &quot;:/tetrixwindow.js&quot;);</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>     TetrixUiLoader loader;
     QFile uiFile(&quot;:/tetrixwindow.ui&quot;);
     uiFile.open(QIODevice::ReadOnly);
     QWidget *ui = loader.load(&amp;uiFile);
     uiFile.close();

     QScriptValue ctor = engine.evaluate(&quot;TetrixWindow&quot;);
     QScriptValue scriptUi = engine.newQObject(ui, QScriptEngine::ScriptOwnership);
     QScriptValue tetrix = ctor.construct(QScriptValueList() &lt;&lt; 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>     ui-&gt;resize(550, 370);
     ui-&gt;show();

     qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
     return app.exec();</pre>
<p>The form is shown, and the event loop is entered.</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="40%" align="left">Copyright &copy; 2010 Nokia Corporation and/or its subsidiary(-ies)</td>
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="40%" align="right"><div align="right">Qt 4.6.3</div></td>
</tr></table></div></address></body>
</html>