|  | Home · All Namespaces · All Classes · Main Classes · Grouped Classes · Modules · Functions |

# QtScript Tetrix Example

Files:

- script/tetrix/tetrixboard.cpp
- script/tetrix/tetrixboard.h
- script/tetrix/tetrixboard.js
- script/tetrix/tetrixpiece.js
- script/tetrix/tetrixwindow.js
- script/tetrix/tetrixwindow.ui
- script/tetrix/main.cpp
- script/tetrix/tetrix.pro
- script/tetrix/tetrix.qrc

The Tetrix example is a QtScript version of the classic Tetrix game.

The program logic in this example is a fairly straight port of the logic in the C++ Tetrix Example. The graphical user interface is defined in a UI file.

     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);
         }
     };

We define a custom UI loader that handles our TetrixBoard widget; the TetrixBoard is the main component of the UI (where the pieces are drawn).

         QApplication app(argc, argv);
         QScriptEngine engine;

         QScriptValue Qt = engine.newQMetaObject(QtMetaObject::get());
         Qt.setProperty("App", engine.newQObject(&app));
         engine.globalObject().setProperty("Qt", Qt);

We initialize the script engine to have the Qt namespace, so that e.g. Qt.Key_Left will be available to script code. We also make the application object available (for the quit() slot).

         evaluateFile(engine, ":/tetrixpiece.js");
         evaluateFile(engine, ":/tetrixboard.js");
         evaluateFile(engine, ":/tetrixwindow.js");

Several scripts are evaluated as part of the engine setup process. tetrixpiece.js contains the definition of the TetrixPiece class, which is used to populate the play field. tetrixboard.js contains the definition of the TetrixBoard class, which contains the main game logic. Finally, tetrixwindow.js contains the definition of the TetrixWindow class, which wires up the top-level widget.

         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);

A form is created from the UI file. A new TetrixWindow script object is then constructed, passing the form as argument.

         ui->resize(550, 370);
         ui->show();

         qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
         return app.exec();

The form is shown, and the event loop is entered.