<?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"> </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></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 &className, QWidget *parent = 0, const QString &name = QString()) { if (className == QLatin1String("TetrixBoard")) { QWidget *board = new TetrixBoard(parent); board->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("App", engine.newQObject(&app)); engine.globalObject().setProperty("Qt", Qt);</pre> <p>We initialize the script engine to have the Qt namespace, so that e.g., <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, ":/tetrixpiece.js"); evaluateFile(engine, ":/tetrixboard.js"); evaluateFile(engine, ":/tetrixwindow.js");</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(":/tetrixwindow.ui"); uiFile.open(QIODevice::ReadOnly); QWidget *ui = loader.load(&uiFile); uiFile.close(); QScriptValue ctor = engine.evaluate("TetrixWindow"); QScriptValue scriptUi = engine.newQObject(ui, QScriptEngine::ScriptOwnership); QScriptValue tetrix = ctor.construct(QScriptValueList() << 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->resize(550, 370); ui->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 © 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>